Comparing version 0.1.8 to 0.1.9
@@ -14,15 +14,36 @@ "use strict"; | ||
this.scope = this.visitor.scope; | ||
const console_polyfill = { | ||
log: (args) => console.log(...args) | ||
}; | ||
const string_polyfill = { | ||
fromCharCode: (num) => String.fromCharCode(num) | ||
}; | ||
const math_polyfill = { | ||
pow: (args) => Math.pow(...args) | ||
}; | ||
this.scope.set('console', console_polyfill); | ||
this.scope.set('String', string_polyfill); | ||
this.scope.set('Math', math_polyfill); | ||
this.scope.set('Date', Date); | ||
this.visitor.on('console', (node, visitor) => { | ||
if (node.type === 'Identifier') | ||
return console; | ||
const prop = visitor.visitNode(node.callee.property); | ||
const args = node.arguments.map((arg) => visitor.visitNode(arg)); | ||
const console_prop = console[prop]; | ||
if (!console_prop) | ||
return 'proceed'; | ||
return console_prop(...args); | ||
}); | ||
this.visitor.on('Math', (node, visitor) => { | ||
if (node.type === 'Identifier') | ||
return Math; | ||
const prop = visitor.visitNode(node.callee.property); | ||
const args = node.arguments.map((arg) => visitor.visitNode(arg)); | ||
const math_prop = Math[prop]; | ||
if (!math_prop) | ||
return 'proceed'; | ||
return math_prop(...args); | ||
}); | ||
this.visitor.on('String', (node, visitor) => { | ||
if (node.type === 'Identifier') | ||
return String; | ||
const prop = visitor.visitNode(node.callee.property); | ||
const args = node.arguments.map((arg) => visitor.visitNode(arg)); | ||
const string_prop = String[prop]; | ||
if (!string_prop) | ||
return 'proceed'; | ||
return string_prop(args); | ||
}); | ||
this.visitor.on('Date', (node) => { | ||
if (node.type === 'Identifier') | ||
return Date; | ||
}); | ||
} | ||
@@ -29,0 +50,0 @@ interpret() { |
@@ -13,3 +13,4 @@ "use strict"; | ||
stmt.type === 'IfStatement' || | ||
stmt.type === 'ForStatement') && | ||
stmt.type === 'ForStatement' || | ||
stmt.type === 'TryStatement') && | ||
!!result) { | ||
@@ -16,0 +17,0 @@ return result; |
@@ -5,4 +5,13 @@ "use strict"; | ||
static visit(node, visitor) { | ||
var _a; | ||
const fn_name = node.callee.name || ((_a = node.callee.property) === null || _a === void 0 ? void 0 : _a.name); | ||
var _a, _b; | ||
const obj_name = (_a = node.callee.object) === null || _a === void 0 ? void 0 : _a.name; | ||
const fn_name = node.callee.name || ((_b = node.callee.property) === null || _b === void 0 ? void 0 : _b.name); | ||
// Obj.fn(...); | ||
if (visitor.listeners[obj_name]) { | ||
const cb = visitor.listeners[obj_name](node, visitor); | ||
if (cb !== 'proceed') { | ||
return cb; | ||
} | ||
} | ||
// ?.fn(...); | ||
if (visitor.listeners[fn_name]) { | ||
@@ -65,3 +74,3 @@ const cb = visitor.listeners[fn_name](node, visitor); | ||
const arr = visitor.visitNode(node.callee.object); | ||
// Set forEach's visitorArg | ||
// Set forEach's “this” arg | ||
if (args.length > 1) { | ||
@@ -88,2 +97,6 @@ visitor.scope.set('_this', args.slice(-1)[0]); | ||
const args = node.arguments.map((arg) => visitor.visitNode(arg)); | ||
if (callee.toString().includes('[native code]')) { | ||
const obj = visitor.visitNode(node.callee.object); | ||
return obj[node.callee.property.name](); | ||
} | ||
return callee(args); | ||
@@ -90,0 +103,0 @@ } |
@@ -6,3 +6,6 @@ "use strict"; | ||
if (visitor.listeners[node.name]) { | ||
return visitor.listeners[node.name](node, visitor); | ||
const cb = visitor.listeners[node.name](node, visitor); | ||
if (cb !== 'proceed') { | ||
return cb; | ||
} | ||
} | ||
@@ -9,0 +12,0 @@ if (visitor.scope.has(node.name)) |
{ | ||
"name": "jintr", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A tiny JavaScript interpreter written in TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
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
37473
967