MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add eval

+102 -1
+1 -1
meson.build
··· 41 41 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 42 42 43 43 version_conf = configuration_data() 44 - version_conf.set('ANT_VERSION', '0.0.5.49') 44 + version_conf.set('ANT_VERSION', '0.0.5.50') 45 45 version_conf.set('ANT_GIT_HASH', git_hash) 46 46 version_conf.set('ANT_BUILD_DATE', build_date) 47 47
+29
src/ant.c
··· 4354 4354 return arg; 4355 4355 } 4356 4356 4357 + static jsval_t builtin_eval(struct js *js, jsval_t *args, int nargs) { 4358 + if (nargs == 0) return js_mkundef(); 4359 + 4360 + jsval_t code_arg = args[0]; 4361 + if (vtype(code_arg) != T_STR) return code_arg; 4362 + 4363 + jsoff_t code_len, code_off = vstr(js, code_arg, &code_len); 4364 + const char *code_str = (const char *)&js->mem[code_off]; 4365 + 4366 + const char *saved_code = js->code; 4367 + jsoff_t saved_clen = js->clen; 4368 + jsoff_t saved_pos = js->pos; 4369 + uint8_t saved_tok = js->tok; 4370 + uint8_t saved_consumed = js->consumed; 4371 + uint8_t saved_flags = js->flags; 4372 + 4373 + jsval_t result = js_eval(js, code_str, code_len); 4374 + 4375 + js->code = saved_code; 4376 + js->clen = saved_clen; 4377 + js->pos = saved_pos; 4378 + js->tok = saved_tok; 4379 + js->consumed = saved_consumed; 4380 + js->flags = saved_flags; 4381 + 4382 + return result; 4383 + } 4384 + 4357 4385 static jsval_t builtin_Function(struct js *js, jsval_t *args, int nargs) { 4358 4386 if (nargs == 0) { 4359 4387 jsval_t code_str = js_mkstr(js, "(){}", 4); ··· 5853 5881 jsval_t obj_func = mkval(T_FUNC, vdata(obj_func_obj)); 5854 5882 5855 5883 setprop(js, glob, js_mkstr(js, "Object", 6), obj_func); 5884 + setprop(js, glob, js_mkstr(js, "eval", 4), js_mkfun(builtin_eval)); 5856 5885 setprop(js, glob, js_mkstr(js, "Function", 8), js_mkfun(builtin_Function)); 5857 5886 setprop(js, glob, js_mkstr(js, "String", 6), js_mkfun(builtin_String)); 5858 5887 setprop(js, glob, js_mkstr(js, "Number", 6), js_mkfun(builtin_Number));
+72
tests/test_eval.cjs
··· 1 + // Test eval() function 2 + // eval() executes JavaScript code represented as a string 3 + // and returns the value of the last expression evaluated 4 + 5 + console.log('=== Testing eval() Function ===\n'); 6 + 7 + // Test 1: Basic arithmetic 8 + console.log('Test 1: Basic arithmetic'); 9 + console.log(' eval("1 + 2"):', eval('1 + 2'), '(expected: 3)'); 10 + console.log(' eval("10 * 5"):', eval('10 * 5'), '(expected: 50)'); 11 + 12 + // Test 2: Variable declaration and use 13 + console.log('\nTest 2: Variable declaration and access'); 14 + eval('let x = 42'); 15 + console.log(' After eval("let x = 42"), x =', x, '(expected: 42)'); 16 + 17 + // Test 3: Function definition 18 + console.log('\nTest 3: Function definition via eval'); 19 + eval('function add(a, b) { return a + b; }'); 20 + console.log(' add(3, 4):', add(3, 4), '(expected: 7)'); 21 + 22 + // Test 4: Access to outer scope variables 23 + console.log('\nTest 4: Access to outer scope variables'); 24 + let outerVar = 100; 25 + let result = eval('outerVar + 50'); 26 + console.log(' outerVar = 100, eval("outerVar + 50"):', result, '(expected: 150)'); 27 + 28 + // Test 5: String operations 29 + console.log('\nTest 5: String operations'); 30 + console.log(' eval(\'"Hello" + " " + "World"\'):', eval('"Hello" + " " + "World"'), '(expected: "Hello World")'); 31 + 32 + // Test 6: Object literal 33 + console.log('\nTest 6: Object literal'); 34 + let obj = eval('({a: 1, b: 2})'); 35 + console.log(' eval("({a: 1, b: 2})"):', obj); 36 + console.log(' obj.a:', obj.a, '(expected: 1)'); 37 + console.log(' obj.b:', obj.b, '(expected: 2)'); 38 + 39 + // Test 7: Array literal 40 + console.log('\nTest 7: Array literal'); 41 + let arr = eval('[1, 2, 3, 4, 5]'); 42 + console.log(' eval("[1, 2, 3, 4, 5]"):', arr); 43 + console.log(' arr[0]:', arr[0], '(expected: 1)'); 44 + console.log(' arr.length:', arr.length, '(expected: 5)'); 45 + 46 + // Test 8: Non-string argument (should return as-is) 47 + console.log('\nTest 8: Non-string argument'); 48 + console.log(' eval(42):', eval(42), '(expected: 42)'); 49 + console.log(' eval(true):', eval(true), '(expected: true)'); 50 + 51 + // Test 9: Empty eval 52 + console.log('\nTest 9: Empty eval'); 53 + console.log(' eval():', eval(), '(expected: undefined)'); 54 + 55 + // Test 10: Complex expression 56 + console.log('\nTest 10: Complex expression'); 57 + let complex = eval('let temp = 5; temp * temp + 10'); 58 + console.log(' eval("let temp = 5; temp * temp + 10"):', complex, '(expected: 35)'); 59 + 60 + // Test 11: Modifying existing variable 61 + console.log('\nTest 11: Modifying existing variable'); 62 + let modVar = 10; 63 + console.log(' Before: modVar =', modVar); 64 + eval('modVar = modVar * 2'); 65 + console.log(' After eval("modVar = modVar * 2"): modVar =', modVar, '(expected: 20)'); 66 + 67 + // Test 12: Return value is last expression 68 + console.log('\nTest 12: Return value is last expression'); 69 + let lastExpr = eval('1 + 1; 2 + 2; 3 + 3'); 70 + console.log(' eval("1 + 1; 2 + 2; 3 + 3"):', lastExpr, '(expected: 6)'); 71 + 72 + console.log('\n=== All eval() Tests Complete ===');