From 8cb12c669aa978cf8cc7108ab2348834af7ed617 Mon Sep 17 00:00:00 2001 From: Ulrich Kortenkamp Date: Mon, 5 Sep 2022 13:26:16 +0200 Subject: [PATCH] implemented eval() --- src/js/libcs/Operators.js | 17 +++++++++++++++++ tests/ColonOp_tests.js | 4 ++++ tests/JSON_tests.js | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/js/libcs/Operators.js b/src/js/libcs/Operators.js index 1c0dfa4d5..aa702c552 100644 --- a/src/js/libcs/Operators.js +++ b/src/js/libcs/Operators.js @@ -4372,6 +4372,23 @@ evaluator.self$0 = function (args, modifs) { return Json._helper.self; }; +evaluator.eval$1 = function (args, modifs) { + Object.entries(modifs).forEach(function ([key, value]) { + let val = evaluate(value); + namespace.newvar(key); + namespace.setvar(key, val); + }); + + namespace.pushVstack("*"); + const erg = evaluate(args[0]); + namespace.cleanVstack(); + Object.entries(modifs).forEach(function ([key, value]) { + namespace.removevar(key); + }); + return erg; + // return tt(args,modifs); +}; + /////////////////////////////// // Calling external code // /////////////////////////////// diff --git a/tests/ColonOp_tests.js b/tests/ColonOp_tests.js index 943ed6c3a..25aa15cf2 100644 --- a/tests/ColonOp_tests.js +++ b/tests/ColonOp_tests.js @@ -127,3 +127,7 @@ describe("Assign code to JSON objects", function () { itCmd('obj={};obj:"code":=(x=x*7);x=1;obj.code;obj.code;x', "49"); itCmd('obj={};obj:"code":=(x=x*7);x=1;obj:"code";obj.code;x', "49"); }); + +describe("Calling code through eval", function () { + itCmd("eval(x+y,x->2,y->5)", "7"); +}); diff --git a/tests/JSON_tests.js b/tests/JSON_tests.js index 67242d95b..d2e5422d8 100644 --- a/tests/JSON_tests.js +++ b/tests/JSON_tests.js @@ -135,6 +135,6 @@ describe("JSON operations", function () { "{a:200, b:{c:4, ef:[1, 2, 3, 100], ghi:jkl}}" ); - //JSON code + //JSON code & Self itCmd('json={"name":"Peter"};json:"code":=self():"name";json:"code"', "Peter"); });