basisjs-tools-ast
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,8 +0,5 @@ | ||
var vm = require('vm'); | ||
var parser = require('./parser'); | ||
var translate = require('./translator').gen_code; | ||
var walker = require('./walker').ast_walker(); | ||
var scope = require('./scope'); | ||
var names = require('./names'); | ||
var structure = require('./structure'); | ||
@@ -71,34 +68,3 @@ | ||
function getCallArgs(args, context, flow, file){ | ||
return args.map(function(arg){ | ||
if (arg[0] == 'string') | ||
{ | ||
return arg[1]; | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
var code = translate(arg); | ||
var result = vm.runInNewContext('0,' + code, context); | ||
if (typeof result == 'string' || typeof result == 'object') | ||
return result; | ||
} catch(e) { | ||
if (flow) | ||
flow.warn({ | ||
file: file ? file.relpath : '', | ||
message: 'Unable to evaluate: ' + code | ||
}); | ||
else | ||
console.log('Unable to evaluate: ' + code); | ||
} | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
isReserved: names.isReserved, | ||
resolveName: names.resolveName, | ||
resolveNameRef: names.resolveNameRef, | ||
Scope: scope.Scope, | ||
@@ -121,3 +87,2 @@ applyScope: scope.process, | ||
normalize: normalize, | ||
getCallArgs: getCallArgs, | ||
@@ -124,0 +89,0 @@ isAstEqualsCode: isAstEqualsCode, |
@@ -1182,10 +1182,13 @@ // patched uglifyjs 1.3.5 | ||
break; | ||
var loc = info(S.token) | ||
var type = S.token.type; | ||
var name = as_property_name(); | ||
var token; | ||
if (type == "name" && (name == "get" || name == "set") && !is("punc", ":")) { | ||
a.push([ as_name(), function_(false), name ]); | ||
a.push(token = [ as_name(), function_(false), name ]); | ||
} else { | ||
expect(":"); | ||
a.push([ name, expression(false) ]); | ||
a.push(token = [ name, expression(false) ]); | ||
} | ||
token.start = loc; | ||
} | ||
@@ -1192,0 +1195,0 @@ next(); |
@@ -6,35 +6,48 @@ /* | ||
var walker = require('./walker').ast_walker(); | ||
var primitive = ['true', 'false', 'null']; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var fnWalker = function(token){ | ||
var fnWalker = function(token, scope){ | ||
var type = token[0]; | ||
var name = token[1]; | ||
var args = token[2]; | ||
var newScope = new Scope('function', scope); | ||
newScope.sourceToken = token; | ||
token.scope = newScope; | ||
if (name) | ||
this.scope.put(name, token[0], token, token.start); | ||
{ | ||
if (type == 'defun') | ||
scope.put(name, { | ||
type: type, | ||
token: token, | ||
loc: token.start | ||
}); | ||
else | ||
newScope.put(name, { | ||
type: type, | ||
token: token, | ||
loc: token.start | ||
}); | ||
} | ||
var scope = new Scope('function', this.scope); | ||
scope.sourceToken = token; | ||
this.scopes.push(scope); | ||
newScope.put('arguments', { | ||
type: 'arguments', | ||
token: null, | ||
args: args | ||
}); | ||
scope.put('arguments', 'arguments', null, null, args); | ||
for (var i = 0; i < args.length; i++) | ||
scope.put(args[i], 'arg', null, args.loc && args.loc[i], { list: args, index: i }); | ||
newScope.put(args[i], { | ||
type: 'arg', | ||
token: null, | ||
loc: args.loc && args.loc[i], | ||
args: args, | ||
index: i | ||
}); | ||
token.scope = scope; | ||
this.scopes.push(newScope); | ||
}; | ||
var varWalker = function(token){ | ||
var defs = token[1]; | ||
for (var i = 0, def; def = defs[i]; i++) | ||
{ | ||
var name = def[0]; | ||
var val = def[1]; | ||
this.scope.put(name, 'var', val, def.start); | ||
} | ||
}; | ||
// | ||
@@ -49,2 +62,13 @@ // main function | ||
var varWalker = function(token, scope){ | ||
var defs = token[1]; | ||
for (var i = 0, def; def = defs[i]; i++) | ||
scope.put(def[0], { | ||
type: 'var', | ||
token: def[1], | ||
loc: def.start | ||
}); | ||
}; | ||
return walker.walk(ast, { | ||
@@ -56,18 +80,31 @@ 'var': varWalker, | ||
'try': function(token){ | ||
if (token[2]) | ||
'directive': function(token, scope){ | ||
if (token[1] == 'use strict') | ||
scope.strict = true; | ||
}, | ||
'try': function(token, scope){ | ||
var catchNode = token[2]; | ||
if (catchNode) | ||
{ | ||
var scope = new Scope('catch', this.scope); | ||
var newScope = new Scope('catch', scope); | ||
scope.put(catchNode[0], { | ||
type: 'catch', | ||
token: null | ||
}); | ||
catchNode[1].scope = newScope; | ||
this.scopes.push(scope); | ||
scope.put(token[2][0], 'catch'); | ||
token[2][1].scope = scope; | ||
} | ||
}, | ||
'name': function(token){ | ||
if (primitive.indexOf(token[1]) == -1) | ||
{ | ||
token.scope = this.scope; | ||
this.names.push(token); | ||
} | ||
'name': function(token, scope){ | ||
var name = token[1]; | ||
if (name == 'null' || name == 'false' || name == 'true') | ||
return; | ||
this.names.push({ | ||
scope: scope, | ||
token: token | ||
}); | ||
} | ||
@@ -89,8 +126,9 @@ }, { | ||
this.subscopes = []; | ||
this.names = {}; | ||
if (parentScope) | ||
{ | ||
this.level = parentScope.level + 1; | ||
this.parent = parentScope; | ||
this.root = parentScope.root; | ||
this.strict = parentScope.strict; | ||
parentScope.subscopes.push(this); | ||
@@ -103,12 +141,6 @@ } | ||
if (parentScope) | ||
{ | ||
var Names = function(){}; | ||
Names.prototype = parentScope.names; | ||
this.names = new Names(); | ||
} | ||
else | ||
this.names = {}; | ||
this.put('this', 'readonly', this.thisObject); | ||
this.put('this', { | ||
type: 'readonly', | ||
token: this.thisObject | ||
}); | ||
} | ||
@@ -122,12 +154,4 @@ | ||
names: null, | ||
level: 1, | ||
strict: false, | ||
getNames: function(){ | ||
var result = {}; | ||
for (var name in this.names) | ||
result[name] = this.names[name]; | ||
return result; | ||
}, | ||
getOwnNames: function(){ | ||
@@ -137,16 +161,16 @@ return Object.keys(this.names); | ||
scopeByName: function(name){ | ||
if (name in this.names) | ||
var cursor = this; | ||
while (cursor) | ||
{ | ||
var cursor = this; | ||
while (cursor) | ||
{ | ||
if (hasOwnProperty.call(cursor.names, name)) // hasOwnProperty may be overriden | ||
return cursor; | ||
if (hasOwnProperty.call(cursor.names, name)) | ||
return cursor; | ||
cursor = cursor.parent; | ||
} | ||
cursor = cursor.parent; | ||
} | ||
return null; | ||
}, | ||
has: function(name){ | ||
return !!this.scopeByName(name); | ||
return this.scopeByName(name) != null; | ||
}, | ||
@@ -157,5 +181,9 @@ hasOwn: function(name){ | ||
get: function(name){ | ||
if (this.has(name)) | ||
return this.names[name]; | ||
var ownerScope = this.scopeByName(name); | ||
if (ownerScope) | ||
return ownerScope.names[name]; | ||
}, | ||
getOwn: function(name){ | ||
return this.names[name] || null; | ||
}, | ||
token: function(name){ | ||
@@ -165,11 +193,4 @@ var ref = this.get(name); | ||
}, | ||
put: function(name, type, token, loc, extra){ | ||
var ref = [type, token]; | ||
ref.token = token || null; | ||
ref.loc = loc || null; | ||
ref.extra = extra; | ||
this.names[name] = ref; | ||
return ref; | ||
put: function(name, info){ | ||
this.names[name] = info; | ||
}, | ||
@@ -181,3 +202,2 @@ set: function(name, token){ | ||
var ref = scope.names[name]; | ||
ref[1] = token; | ||
ref.token = token; | ||
@@ -302,2 +322,6 @@ } | ||
break; | ||
case 'array': | ||
return ['array', token[1].map(this.simpleExpression, this)]; | ||
default: | ||
@@ -311,9 +335,5 @@ return this.deepResolve(token); | ||
}, | ||
isSpecial: function(name){ | ||
name = this.get(name); | ||
return name && name.type == 'special'; | ||
}, | ||
isGlobal: function(name){ | ||
var entry = this.get(name); | ||
return entry && ((entry.token && entry.token.scope && entry.token.scope == this.root) || entry === this.root.get(name)); | ||
var scope = this.scopeByName(name); | ||
return scope ? scope.root === scope : true; | ||
} | ||
@@ -320,0 +340,0 @@ }; |
@@ -8,3 +8,3 @@ var walker = require('./walker').ast_walker(); | ||
return walker.walk(ast, { | ||
'call': function(token){ | ||
'call': function(token, scope){ | ||
var expr = token[1]; | ||
@@ -16,6 +16,8 @@ var args = token[2]; | ||
var fn = this.scope.resolve(expr); | ||
var fn = scope.resolve(expr); | ||
if (fn) | ||
{ | ||
var verbose = context.console.enabled; | ||
if (fn.ref_) | ||
@@ -28,7 +30,15 @@ fn = fn.ref_; | ||
{ | ||
context.console.start('> ' + translate(token)); | ||
fn.run.call(this, token, expr[0] == 'dot' || expr[0] == 'name' ? this.scope.resolve(expr[1]) : null, args.map(function(a){ | ||
return this.resolve(a) || a; | ||
}, this.scope)); | ||
context.console.end(); | ||
if (verbose) | ||
context.console.start('> ' + translate(token)); | ||
fn.run.call( | ||
this, | ||
token, | ||
expr[0] == 'dot' || expr[0] == 'name' ? scope.resolve(expr[1]) : null, | ||
args, | ||
scope | ||
); | ||
if (verbose) | ||
context.console.end(); | ||
} | ||
@@ -40,5 +50,15 @@ } | ||
{ | ||
context.console.start('> ' + translate(token)); | ||
fn.call.run.call(this, token, fn.call, args.map(this.scope.resolve, this.scope)); | ||
context.console.end(); | ||
if (verbose) | ||
context.console.start('> ' + translate(token)); | ||
fn.call.run.call( | ||
this, | ||
token, | ||
fn.call, | ||
args, | ||
scope | ||
); | ||
if (verbose) | ||
context.console.end(); | ||
} | ||
@@ -60,28 +80,3 @@ } | ||
return token; | ||
},/*, | ||
'for-in': function(token){ | ||
var v = token[1]; | ||
var key = token[2][1]; | ||
var obj = this.scope.resolve(token[3]); | ||
debugger; | ||
if (obj) | ||
{ | ||
var oo = obj.obj; | ||
for (var k in oo) | ||
{ | ||
var val = ['string', k]; | ||
val.obj = k; | ||
this.scope.put(key, 'var', val); | ||
this.walk(token, 4); | ||
} | ||
} | ||
}, | ||
'if': function(token){ | ||
var res = this.walk(token, 1).obj; | ||
if (res && res[0] == 'name' && res[1] == 'true') | ||
this.walk(token, 2); | ||
},*/ | ||
'defun': function(token){ | ||
token.parent_ = this.top(1); | ||
}, | ||
'dot': function(token){ | ||
@@ -98,7 +93,6 @@ if (token.ref_) | ||
token.ref_ = obj && obj[token[2]]; | ||
token.refPath_ = path.refPath_ + '.' + token[2]; | ||
} | ||
return token; | ||
}, | ||
'name': function(token){ | ||
'name': function(token, scope){ | ||
if (token.ref_) | ||
@@ -108,31 +102,25 @@ return; | ||
var name = token[1]; | ||
if (this.scope.isGlobal(name) && name != 'global' && name != 'this') // TODO: make correct test for global | ||
if (scope.isGlobal(name) && name != 'global' && name != 'this') // TODO: make correct test for global | ||
{ | ||
var ref = this.scope.get(name); | ||
var ref = scope.get(name); | ||
if (ref) | ||
{ | ||
token.ref_ = ref.token; | ||
token.refPath_ = name; | ||
} | ||
} | ||
else | ||
{ | ||
var ref = this.scope.resolve(token); | ||
var ref = scope.resolve(token); | ||
if (ref && ref.ref_) | ||
{ | ||
token.ref_ = ref.ref_; | ||
token.refPath_ = ref.refPath_; | ||
} | ||
} | ||
}, | ||
'object': function(token){ | ||
token.obj = {}; | ||
token.objSource = {}; | ||
for (var i = 0, prop; prop = token[1][i]; i++) | ||
{ | ||
token.obj[prop[0]] = this.scope.resolve(prop[1]) || prop[1]; | ||
token.objSource[prop[0]] = token; | ||
} | ||
'object': function(token, scope){ | ||
var props = token[1]; | ||
var obj = {}; | ||
for (var i = 0, prop; prop = props[i]; i++) | ||
obj[prop[0]] = scope.resolve(prop[1]) || prop[1]; | ||
token.obj = obj; | ||
}, | ||
'assign': function(token){ | ||
'assign': function(token, scope){ | ||
var op = token[1]; | ||
@@ -147,3 +135,3 @@ var lvalue = token[2]; | ||
rvalue = this.walk(rvalue); | ||
this.scope.set(lvalue[1], rvalue); | ||
scope.set(lvalue[1], rvalue); | ||
@@ -157,6 +145,6 @@ return token; | ||
var dest = this.scope.resolve(lvalue[1]); | ||
var dest = scope.resolve(lvalue[1]); | ||
if (dest && dest.obj) | ||
dest.obj[lvalue[2]] = this.scope.resolve(rvalue) || rvalue; | ||
dest.obj[lvalue[2]] = scope.resolve(rvalue) || rvalue; | ||
@@ -163,0 +151,0 @@ return token; |
@@ -61,8 +61,4 @@ function overrideObject(obj, props){ | ||
{ | ||
walkerContext.scope = scope; | ||
var ret = userFn.call(walkerContext, token, scope); | ||
var ret = userFn.call(walkerContext, token); | ||
walkerContext.scope = storedScope; | ||
if (ret != null) | ||
@@ -322,4 +318,3 @@ { | ||
stack: stack, | ||
top: top, | ||
scope: null | ||
top: top | ||
}; | ||
@@ -326,0 +321,0 @@ |
{ | ||
"name": "basisjs-tools-ast", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Tool set for basisjs-tools to work with various AST", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/basisjs/basisjs-tools-ast", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
0
184928
16
5438