Skip to content

Commit 010e98d

Browse files
committed
Fixe bug with empty strings #11
1 parent 02033e4 commit 010e98d

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<div class="container">
2929
<h4>JSPython development console</h4>
3030
<div id="editor">
31-
x = """issue = {}"""
32-
x</div>
31+
"".trim().length
32+
</div>
3333
<button onclick="runInterpreter()">Run</button>
3434
<textarea id="result"></textarea>
3535
</div>
@@ -48,7 +48,7 @@ <h4>JSPython development console</h4>
4848
const result = await jsPython()
4949
.addFunction('returnsPromise', a1 => new Promise((s, f) => { setTimeout(() => s(a1), 10) }))
5050
.addFunction('nullValue', () => { console.log(' ** invoked!!!'); return null })
51-
.evaluate(scripts);
51+
.evaluate(scripts, {str: "shdsd sd,sd d s ds d"});
5252
document.getElementById('result').value = typeof result === 'object' ? JSON.stringify(result) : result
5353
console.log('Result => ', result);
5454
} catch (err) {

src/eval/eval.expression.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ export class EvalExpression {
272272
}
273273
}
274274

275-
if (parentObject) {
275+
if (parentObject !== null && parentObject !== undefined) {
276276
const value = getValue(parentObject, token);
277277
return (value !== undefined) ? value : null
278278

279279
} else {
280280
const value = getValue(context.blockScope, token);
281281
if (value === undefined) {
282-
throw Error(`Undefined property '${token}'`);
282+
throw Error(`Can't resolve property '${token}' of undefined object`);
283283
}
284284
return value;
285285
}
@@ -375,16 +375,16 @@ export class EvalExpression {
375375
token = token.trim();
376376
const findFunction = (name: string, obj: any = null): AnyFunc => {
377377
if (name.indexOf('.') < 0) {
378-
if (!obj) {
378+
if (obj === null || obj === undefined) {
379379
const fn = context.blockScope[name];
380380
if (typeof fn !== 'function') {
381-
throw Error(`Token '${name}' is not a valid function (1)`);
381+
throw Error(`Can't call '${name}' of null (1)`);
382382
}
383383
return fn; // all functions should be here and no 'bind' required
384384
} else {
385385
const ff = obj[name];
386386
if (typeof ff !== 'function') {
387-
throw Error(`Token '${name}' is not a valid function (2)`);
387+
throw Error(`Can't call '${name}' of null (2)`);
388388
}
389389
return ff.bind(obj);
390390
}

src/interpreter.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,5 +1481,13 @@ issue["dd22"]
14811481
`)).toBe(55);
14821482
});
14831483

1484+
it('Empty string bug', async () => {
1485+
expect(await e.evaluate(`"".trim()`)).toBe("");
1486+
});
1487+
1488+
it('Empty string bug for function return', async () => {
1489+
expect(await e.evaluate(`"".trim().length`)).toBe(0);
1490+
});
1491+
14841492

14851493
});

0 commit comments

Comments
 (0)