krl-compiler
Advanced tools
Comparing version 0.29.3 to 0.30.0
{ | ||
"name": "krl-compiler", | ||
"version": "0.29.3", | ||
"version": "0.30.0", | ||
"description": "KRL compiler", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
module.exports = function(ast, comp, e){ | ||
return e(";", e("call", e("id", "ctx.log"), [ | ||
ast.level | ||
? e("string", ast.level) | ||
: e("null"), | ||
comp(ast.expression) | ||
e("string", ast.level), | ||
comp(ast.expression), | ||
])); | ||
}; |
@@ -1,3 +0,1 @@ | ||
var callModuleFn = require("../utils/callModuleFn"); | ||
module.exports = function(ast, comp, e){ | ||
@@ -13,3 +11,3 @@ | ||
return e(";", callModuleFn(e, "event", "raise", e("obj", args), ast.loc)); | ||
return e(";", e("ycall", e("id", "ctx.raiseEvent"), [e("obj", args)])); | ||
}; |
var _ = require("lodash"); | ||
var mkTree = require("estree-builder"); | ||
var callStdLibFn = require("./utils/callStdLibFn"); | ||
var callModuleFn = require("./utils/callModuleFn"); | ||
var mkKRLClosure = require("./utils/mkKRLClosure"); | ||
var comp_by_type = { | ||
"String": function(ast, comp, e){ | ||
return e("string", ast.value); | ||
}, | ||
"Number": function(ast, comp, e){ | ||
if(ast.value < 0){ | ||
return e("-", e("number", -ast.value)); | ||
} | ||
return e("number", ast.value); | ||
}, | ||
"Identifier": function(ast, comp, e, context){ | ||
if(ast.value === "null"){ | ||
//undefined is the true "null" in javascript | ||
//however, undefined can be re-assigned. So `void 0` returns undefined but can"t be re-assigned | ||
return e("void", e("number", 0)); | ||
} | ||
if(context && context.identifiers_are_event_attributes){ | ||
return callModuleFn(e, "event", "attr", e("array", [e("str", ast.value)]), ast.loc); | ||
} | ||
return e("call", e("id", "ctx.scope.get"), [e("str", ast.value)]); | ||
}, | ||
"Chevron": function(ast, comp, e){ | ||
if(ast.value.length < 1){ | ||
return e("string", ""); | ||
} | ||
var compElm = function(elm){ | ||
if(elm.type === "String"){ | ||
return e("string", elm.value, elm.loc); | ||
} | ||
return callStdLibFn(e, "beesting", [comp(elm)], elm.loc); | ||
}; | ||
var curr = compElm(ast.value[0]); | ||
var i = 1; | ||
while(i < ast.value.length){ | ||
curr = e("+", curr, compElm(ast.value[i])); | ||
i++; | ||
} | ||
return curr; | ||
}, | ||
"Boolean": function(ast, comp, e){ | ||
return e(ast.value ? "true" : "false"); | ||
}, | ||
"RegExp": function(ast, comp, e){ | ||
var flags = ""; | ||
if(ast.value.global){ | ||
flags += "g"; | ||
} | ||
if(ast.value.ignoreCase){ | ||
flags += "i"; | ||
} | ||
return e("new", e("id", "RegExp"), [ | ||
e("str", ast.value.source), | ||
e("str", flags) | ||
]); | ||
}, | ||
"DomainIdentifier": function(ast, comp, e){ | ||
return e("ycall", e("id", "ctx.modules.get"), [ | ||
e("id", "ctx"), | ||
e("str", ast.domain), | ||
e("str", ast.value) | ||
]); | ||
}, | ||
"MemberExpression": function(ast, comp, e){ | ||
if(ast.method === "dot"){ | ||
if(ast.property.type === "Identifier"){ | ||
//using "get" rather than . b/c we don"t want to mess | ||
//with reserved javascript properties i.e. "prototype" | ||
return e("get", comp(ast.object), e("str", ast.property.value, ast.property.loc)); | ||
} | ||
return e("get", comp(ast.object), comp(ast.property)); | ||
}else if(ast.method === "path"){ | ||
return callStdLibFn(e, "get", [ | ||
comp(ast.object), | ||
comp(ast.property) | ||
], ast.loc); | ||
}else if(ast.method === "index"){ | ||
return callStdLibFn(e, "get", [ | ||
comp(ast.object), | ||
e("array", [comp(ast.property)], ast.property.loc) | ||
], ast.loc); | ||
} | ||
throw new Error("Unsupported MemberExpression method: " + ast.method); | ||
}, | ||
"Array": function(ast, comp, e){ | ||
return e("array", comp(ast.value)); | ||
}, | ||
"Map": function(ast, comp, e){ | ||
return e("obj-raw", comp(ast.value)); | ||
}, | ||
"MapKeyValuePair": function(ast, comp, e){ | ||
return e("obj-prop", e("string", ast.key.value + "", ast.key.loc), comp(ast.value)); | ||
}, | ||
"Application": function(ast, comp, e){ | ||
if(ast.callee.type === "MemberExpression" | ||
&& ast.callee.method === "dot" | ||
&& ast.callee.property.type === "Identifier" | ||
){ | ||
//operator syntax is just sugar for stdlib functions | ||
var operator = ast.callee.property; | ||
var args = [comp(ast.callee.object)].concat(comp(ast.args)); | ||
return callStdLibFn(e, operator.value, args, operator.loc); | ||
} | ||
var args_obj; | ||
if(_.isEmpty(ast.with)){ | ||
args_obj = e("array", comp(ast.args)); | ||
}else{ | ||
var obj = {}; | ||
_.each(ast.args, function(arg, i){ | ||
obj[i] = comp(arg); | ||
}); | ||
_.each(ast.with, function(w){ | ||
if(w.left.type !== "Identifier"){ | ||
throw new Error("`with` only allows keys to be identifiers"); | ||
} | ||
obj[w.left.value] = comp(w.right); | ||
}); | ||
args_obj = e("obj", obj); | ||
} | ||
return e("ycall", comp(ast.callee), [ | ||
e("id", "ctx"), | ||
args_obj | ||
]); | ||
}, | ||
"UnaryOperator": function(ast, comp, e){ | ||
if(ast.op === "not"){ | ||
return e("!", comp(ast.arg)); | ||
} | ||
return callStdLibFn(e, ast.op, [ | ||
comp(ast.arg) | ||
], ast.loc); | ||
}, | ||
"InfixOperator": function(ast, comp, e){ | ||
if((ast.op === "||") || (ast.op === "&&")){ | ||
return e(ast.op, comp(ast.left), comp(ast.right)); | ||
} | ||
return callStdLibFn(e, ast.op, [ | ||
comp(ast.left), | ||
comp(ast.right) | ||
], ast.loc); | ||
}, | ||
"ConditionalExpression": function(ast, comp, e){ | ||
return e("ternary", | ||
comp(ast.test), | ||
comp(ast.consequent), | ||
comp(ast.alternate) | ||
); | ||
}, | ||
"Function": function(ast, comp, e){ | ||
var body = _.map(ast.params, function(param, i){ | ||
var loc = param.loc; | ||
return e(";", e("call", e("id", "ctx.scope.set", loc), [ | ||
e("str", param.value, loc), | ||
e("call", | ||
e("id", "getArg", loc), | ||
[ | ||
e("string", param.value, loc), | ||
e("number", i, loc) | ||
], | ||
loc | ||
) | ||
], loc), loc); | ||
}); | ||
_.each(ast.body, function(part, i){ | ||
if(i < (ast.body.length - 1)){ | ||
return body.push(comp(part)); | ||
} | ||
if(part.type === "ExpressionStatement"){ | ||
part = part.expression; | ||
} | ||
return body.push(e("return", comp(part))); | ||
}); | ||
return mkKRLClosure(e, body); | ||
}, | ||
"PersistentVariableAssignment": function(ast, comp, e){ | ||
if(ast.op !== ":="){ | ||
throw new Error("Unsuported PersistentVariableAssignment.op: " + ast.op); | ||
} | ||
if(ast.left.type !== "DomainIdentifier" || !/^(ent|app)$/.test(ast.left.domain)){ | ||
throw new Error("PersistentVariableAssignment - only works on ent:* or app:* variables"); | ||
} | ||
var comp_by_type = _.fromPairs(_.map([ | ||
"Application", | ||
"Array", | ||
"Boolean", | ||
"Chevron", | ||
"ConditionalExpression", | ||
"Declaration", | ||
"DefAction", | ||
"DomainIdentifier", | ||
"ErrorStatement", | ||
"EventExpression", | ||
"EventWithin", | ||
"ExpressionStatement", | ||
"Function", | ||
"GuardCondition", | ||
"Identifier", | ||
"InfixOperator", | ||
"LastStatement", | ||
"LogStatement", | ||
"Map", | ||
"MapKeyValuePair", | ||
"MemberExpression", | ||
"Number", | ||
"PersistentVariableAssignment", | ||
"RaiseEventAttributes", | ||
"RaiseEventStatement", | ||
"RegExp", | ||
"Rule", | ||
"RuleAction", | ||
"RuleActionBlock", | ||
"RuleForEach", | ||
"RulePostlude", | ||
"RuleSelect", | ||
"Ruleset", | ||
"RulesetID", | ||
"RulesetMeta", | ||
"ScheduleEventStatement", | ||
"String", | ||
"UnaryOperator", | ||
], function(type){ | ||
return [type, require("./c/" + type)]; | ||
})); | ||
var value_to_store = comp(ast.right); | ||
if(ast.path_expression){ | ||
value_to_store = callStdLibFn(e, "set", [ | ||
e("ycall", e("id", "ctx.modules.get"), [ | ||
e("id", "ctx"), | ||
e("str", ast.left.domain), | ||
e("str", ast.left.value) | ||
]), | ||
comp(ast.path_expression), | ||
value_to_store | ||
], ast.loc); | ||
} | ||
return e(";", e("ycall", e("id", "ctx.modules.set"), [ | ||
e("id", "ctx"), | ||
e("str", ast.left.domain, ast.left.loc), | ||
e("str", ast.left.value, ast.left.loc), | ||
value_to_store | ||
])); | ||
}, | ||
"Declaration": require("./c/Declaration"), | ||
"DefAction": require("./c/DefAction"), | ||
"LogStatement": require("./c/LogStatement"), | ||
"ExpressionStatement": function(ast, comp, e){ | ||
return e(";", comp(ast.expression)); | ||
}, | ||
"GuardCondition": require("./c/GuardCondition"), | ||
"Ruleset": function(ast, comp, e){ | ||
var rules_obj = {}; | ||
_.each(ast.rules, function(rule){ | ||
rules_obj[rule.name.value] = comp(rule); | ||
}); | ||
var rs = { | ||
rid: comp(ast.rid) | ||
}; | ||
if(ast.meta){ | ||
rs.meta = comp(ast.meta); | ||
} | ||
if(!_.isEmpty(ast.global)){ | ||
rs.global = e("genfn", ["ctx"], comp(ast.global)); | ||
} | ||
rs.rules = e("obj", rules_obj); | ||
return [ | ||
e(";", e("=", e("id", "module.exports"), e("obj", rs))) | ||
]; | ||
}, | ||
"RulesetID": function(ast, comp, e){ | ||
return e("string", ast.value); | ||
}, | ||
"RulesetMeta": require("./c/RulesetMeta"), | ||
"Rule": function(ast, comp, e){ | ||
var rule = { | ||
name: e("string", ast.name.value, ast.name.loc) | ||
}; | ||
if(ast.rule_state !== "active"){ | ||
rule.rule_state = e("string", ast.rule_state); | ||
} | ||
if(ast.select){ | ||
rule.select = comp(ast.select); | ||
} | ||
if(!_.isEmpty(ast.foreach)){ | ||
var nestedForeach = function(arr, iter){ | ||
if(_.isEmpty(arr)){ | ||
return iter; | ||
} | ||
var last = _.last(arr); | ||
var rest = _.initial(arr); | ||
return nestedForeach(rest, comp(last, {iter: iter})); | ||
}; | ||
rule.foreach = e("genfn", ["ctx", "foreach", "iter"], [ | ||
nestedForeach(ast.foreach, e(";", e("ycall", e("id", "iter"), [e("id", "ctx")]))) | ||
]); | ||
} | ||
if(!_.isEmpty(ast.prelude)){ | ||
rule.prelude = e("genfn", ["ctx"], comp(ast.prelude)); | ||
} | ||
if(ast.action_block){ | ||
rule.action_block = comp(ast.action_block); | ||
} | ||
if(ast.postlude){ | ||
rule.postlude = comp(ast.postlude); | ||
} | ||
return e("obj", rule); | ||
}, | ||
"RuleSelect": require("./c/RuleSelect"), | ||
"RuleForEach": require("./c/RuleForEach"), | ||
"EventWithin": require("./c/EventWithin"), | ||
"EventExpression": require("./c/EventExpression"), | ||
"RuleActionBlock": function(ast, comp, e){ | ||
var block = {}; | ||
if(_.isString(ast.block_type) && ast.block_type !== "every"){ | ||
block.block_type = e("string", ast.block_type); | ||
} | ||
if(ast.condition){ | ||
block.condition = e("genfn", ["ctx"], [ | ||
e("return", comp(ast.condition)) | ||
]); | ||
} | ||
block.actions = e("arr", _.map(ast.actions, function(action){ | ||
return comp(action); | ||
})); | ||
return e("obj", block); | ||
}, | ||
"RuleAction": require("./c/RuleAction"), | ||
"RulePostlude": require("./c/RulePostlude"), | ||
"ScheduleEventStatement": require("./c/ScheduleEventStatement"), | ||
"RaiseEventStatement": require("./c/RaiseEventStatement"), | ||
"RaiseEventAttributes": require("./c/RaiseEventAttributes") | ||
}; | ||
var isKRL_loc = function(loc){ | ||
@@ -300,0 +48,0 @@ return _.isPlainObject(loc) && _.has(loc, "start") && _.has(loc, "end"); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
47
1312
48409
1