Skip to content

Commit

Permalink
[Bug] Fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed Apr 13, 2019
1 parent d171e56 commit 7517f44
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
35 changes: 20 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@
});
}

var BREAK = {};
var CONTINUE = {};
var RETURN = { RES: undefined };
var SUPER = createSymbol('super');
var ASYNC = createSymbol('async');
var NOINIT = createSymbol('noinit');

var Scope = (function () {
function Scope(parent, isolated) {
if (parent === void 0) { parent = null; }
Expand Down Expand Up @@ -567,11 +574,11 @@
}
var variable = scope.context[name];
if (!variable) {
scope.context[name] = new Var('var', value);
scope.context[name] = new Var('var', value === NOINIT ? undefined : value);
}
else {
if (variable.kind === 'var') {
if (value !== undefined) {
if (value !== NOINIT) {
variable.set(value);
}
}
Expand All @@ -581,7 +588,7 @@
}
if (!scope.parent) {
var win = scope.find('window').get();
if (value !== undefined) {
if (value !== NOINIT) {
win[name] = value;
}
}
Expand Down Expand Up @@ -717,21 +724,25 @@
if (!(kind === 'var'
|| kind === 'let'
|| kind === 'const')) return [3, 13];
if (!(typeof feed === 'undefined')) return [3, 7];
return [5, __values(evaluate(node.init, scope))];
case 6:
_c = _d.sent();
if (!('feed' in options)) return [3, 6];
_c = feed;
return [3, 8];
case 6: return [5, __values(evaluate(node.init, scope))];
case 7:
_c = feed;
_c = _d.sent();
_d.label = 8;
case 8:
value = _c;
if (!(node.id.type === 'Identifier')) return [3, 10];
return [5, __values(Identifier(node.id, scope, { getName: true }))];
case 9:
name_2 = _d.sent();
scope[kind](name_2, value);
if (kind === 'var' && !node.init) {
scope.var(name_2, NOINIT);
}
else {
scope[kind](name_2, value);
}
return [3, 12];
case 10: return [5, __values(pattern$1(node.id, scope, { kind: kind, feed: value }))];
case 11:
Expand Down Expand Up @@ -863,12 +874,6 @@
MethodDefinition: MethodDefinition
});

var BREAK = {};
var CONTINUE = {};
var RETURN = { RES: undefined };
var SUPER = createSymbol('super');
var ASYNC = createSymbol('async');

function Literal(node, scope) {
return __generator(this, function (_a) {
return [2, node.value];
Expand Down
2 changes: 1 addition & 1 deletion dist/sval.min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/evaluate/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { VarKind } from '../scope/variable'
import { define, getDptor, assign } from '../share/util'

import { Identifier } from './identifier'
import { NOINIT } from '../share/const'

export function* FunctionDeclaration(node: estree.FunctionDeclaration, scope: Scope) {
scope.func(node.id.name, yield* createFunc(node, scope))
Expand Down Expand Up @@ -51,10 +52,14 @@ export function* VariableDeclarator(
|| kind === 'let'
|| kind === 'const'
) {
const value = typeof feed === 'undefined' ? yield* evaluate(node.init, scope) : feed
const value = 'feed' in options ? feed : yield* evaluate(node.init, scope)
if (node.id.type === 'Identifier') {
const name = yield* Identifier(node.id, scope, { getName: true })
scope[kind](name, value)
if (kind === 'var' && !node.init) {
scope.var(name, NOINIT)
} else {
scope[kind](name, value)
}
} else {
yield* pattern(node.id, scope, { kind, feed: value })
}
Expand Down
7 changes: 4 additions & 3 deletions src/scope/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Variable, Var, Prop } from './variable'
import { hasOwn, getOwnNames } from '../share/util'
import { NOINIT } from '../share/const'

/**
* Scope simulation class
Expand Down Expand Up @@ -102,10 +103,10 @@ export default class Scope {

const variable = scope.context[name]
if (!variable) {
scope.context[name] = new Var('var', value)
scope.context[name] = new Var('var', value === NOINIT ? undefined : value)
} else {
if (variable.kind === 'var') {
if (value !== undefined) {
if (value !== NOINIT) {
variable.set(value)
}
} else {
Expand All @@ -115,7 +116,7 @@ export default class Scope {

if (!scope.parent) {
const win = scope.find('window').get()
if (value !== undefined) {
if (value !== NOINIT) {
win[name] = value
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/share/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export const RETURN: { RES: any } = { RES: undefined }

export const SUPER = createSymbol('super')

export const ASYNC = createSymbol('async')
export const ASYNC = createSymbol('async')

export const NOINIT = createSymbol('noinit')

0 comments on commit 7517f44

Please sign in to comment.