UNPKG

nisp

Version:

A language that for easily build cross-language language

30 lines (29 loc) 964 B
"use strict"; exports.__esModule = true; var core_1 = require("../core"); exports.maxStackSize = 17; function countStack(node) { var count = 0; while (node) { count++; node = node.parent; } return count; } // ["fn", [<arg1>, <arg2>, ...], <exp>] // There are two closures: one is when the function is created, // another one is when the function is called. exports["default"] = core_1.macro(function (ctx) { return function () { if (countStack(this) > exports.maxStackSize) core_1.error(this, 'call stack overflow'); var ast = ctx.ast; // generate a closure sandbox based on the parent sandbox var closure = Object.create(ctx.sandbox); // overwrite arguments to closure for (var i = 0, len = ast[1].length; i < len; i++) { closure[ast[1][i]] = arguments[i]; } return core_1["default"](ast[2], closure, ctx.env, this, ctx.index); }; });