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.

function constructors

+145 -5
+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.48') 44 + version_conf.set('ANT_VERSION', '0.0.5.49') 45 45 version_conf.set('ANT_GIT_HASH', git_hash) 46 46 version_conf.set('ANT_BUILD_DATE', build_date) 47 47
+89 -4
src/ant.c
··· 2555 2555 jsval_t result = js_postfix(js); 2556 2556 jsval_t constructed_obj = js->this_val; 2557 2557 js->this_val = saved_this; 2558 - if (vtype(result) == T_OBJ || vtype(result) == T_ARR || vtype(result) == T_PROMISE) return result; 2558 + if (vtype(result) == T_OBJ || vtype(result) == T_ARR || vtype(result) == T_PROMISE || vtype(result) == T_FUNC) return result; 2559 2559 return constructed_obj; 2560 2560 } else if (next(js) == TOK_DELETE) { 2561 2561 js->consumed = 1; ··· 4355 4355 } 4356 4356 4357 4357 static jsval_t builtin_Function(struct js *js, jsval_t *args, int nargs) { 4358 - (void) args; 4359 - (void) nargs; 4360 - return js_mkundef(); 4358 + if (nargs == 0) { 4359 + jsval_t code_str = js_mkstr(js, "(){}", 4); 4360 + if (is_err(code_str)) return code_str; 4361 + 4362 + jsval_t func_obj = mkobj(js, 0); 4363 + if (is_err(func_obj)) return func_obj; 4364 + 4365 + jsval_t code_key = js_mkstr(js, "__code", 6); 4366 + if (is_err(code_key)) return code_key; 4367 + 4368 + jsval_t res = setprop(js, func_obj, code_key, code_str); 4369 + if (is_err(res)) return res; 4370 + 4371 + jsval_t scope_key = js_mkstr(js, "__scope", 7); 4372 + if (is_err(scope_key)) return scope_key; 4373 + 4374 + res = setprop(js, func_obj, scope_key, js_glob(js)); 4375 + if (is_err(res)) return res; 4376 + 4377 + return mkval(T_FUNC, (unsigned long) vdata(func_obj)); 4378 + } 4379 + 4380 + size_t total_len = 1; 4381 + 4382 + for (int i = 0; i < nargs - 1; i++) { 4383 + if (vtype(args[i]) != T_STR) { 4384 + const char *str = js_str(js, args[i]); 4385 + args[i] = js_mkstr(js, str, strlen(str)); 4386 + if (is_err(args[i])) return args[i]; 4387 + } 4388 + total_len += vstrlen(js, args[i]); 4389 + if (i < nargs - 2) total_len += 1; 4390 + } 4391 + 4392 + total_len += 2; 4393 + 4394 + jsval_t body = args[nargs - 1]; 4395 + if (vtype(body) != T_STR) { 4396 + const char *str = js_str(js, body); 4397 + body = js_mkstr(js, str, strlen(str)); 4398 + if (is_err(body)) return body; 4399 + } 4400 + total_len += vstrlen(js, body); 4401 + total_len += 1; 4402 + 4403 + jsval_t code_str = js_mkstr(js, NULL, total_len); 4404 + if (is_err(code_str)) return code_str; 4405 + 4406 + jsoff_t code_len, code_off = vstr(js, code_str, &code_len); 4407 + char *code_ptr = (char *)&js->mem[code_off]; 4408 + size_t pos = 0; 4409 + 4410 + code_ptr[pos++] = '('; 4411 + 4412 + for (int i = 0; i < nargs - 1; i++) { 4413 + jsoff_t param_len, param_off = vstr(js, args[i], &param_len); 4414 + memcpy(code_ptr + pos, &js->mem[param_off], param_len); 4415 + pos += param_len; 4416 + if (i < nargs - 2) { 4417 + code_ptr[pos++] = ','; 4418 + } 4419 + } 4420 + 4421 + code_ptr[pos++] = ')'; 4422 + code_ptr[pos++] = '{'; 4423 + 4424 + jsoff_t body_len, body_off = vstr(js, body, &body_len); 4425 + memcpy(code_ptr + pos, &js->mem[body_off], body_len); 4426 + pos += body_len; 4427 + 4428 + code_ptr[pos++] = '}'; 4429 + 4430 + jsval_t func_obj = mkobj(js, 0); 4431 + if (is_err(func_obj)) return func_obj; 4432 + 4433 + jsval_t code_key = js_mkstr(js, "__code", 6); 4434 + if (is_err(code_key)) return code_key; 4435 + 4436 + jsval_t res = setprop(js, func_obj, code_key, code_str); 4437 + if (is_err(res)) return res; 4438 + 4439 + jsval_t scope_key = js_mkstr(js, "__scope", 7); 4440 + if (is_err(scope_key)) return scope_key; 4441 + 4442 + res = setprop(js, func_obj, scope_key, js_glob(js)); 4443 + if (is_err(res)) return res; 4444 + 4445 + return mkval(T_FUNC, (unsigned long) vdata(func_obj)); 4361 4446 } 4362 4447 4363 4448 static jsval_t builtin_Array(struct js *js, jsval_t *args, int nargs) {
+55
tests/test_function_constructor.cjs
··· 1 + // Test Function constructor 2 + // This tests whether the Function constructor can create a dynamic function 3 + // from string arguments. 4 + // 5 + // The Function constructor syntax: 6 + // new Function(arg1, arg2, ..., argN, functionBody) 7 + // All arguments are strings. The last argument is the function body, 8 + // and all previous arguments are parameter names. 9 + 10 + console.log('=== Testing Function Constructor ===\n'); 11 + 12 + // Test 1: Basic two-parameter function 13 + console.log('Test 1: Basic addition function'); 14 + let sum = new Function('a', 'b', 'return a + b'); 15 + console.log(' Created function:', sum); 16 + console.log(' typeof sum:', typeof sum); 17 + console.log(' sum(1, 2):', sum(1, 2), '(expected: 3)'); 18 + 19 + // Test 2: No parameters 20 + console.log('\nTest 2: Function with no parameters'); 21 + let hello = new Function('return "Hello World"'); 22 + console.log(' hello():', hello(), '(expected: "Hello World")'); 23 + 24 + // Test 3: Single parameter 25 + console.log('\nTest 3: Single parameter function'); 26 + let square = new Function('x', 'return x * x'); 27 + console.log(' square(5):', square(5), '(expected: 25)'); 28 + 29 + // Test 4: Multiple statements in body 30 + console.log('\nTest 4: Multiple statements in body'); 31 + let multi = new Function('x', 'let y = x * 2; return y + 1'); 32 + console.log(' multi(5):', multi(5), '(expected: 11)'); 33 + 34 + // Test 5: Access to global scope 35 + console.log('\nTest 5: Access to global scope variables'); 36 + let factor = 10; 37 + let multiply = new Function('x', 'return x * factor'); 38 + console.log(' multiply(5):', multiply(5), '(expected: 50)'); 39 + 40 + // Test 6: Empty function body 41 + console.log('\nTest 6: Empty function body'); 42 + let empty = new Function(''); 43 + console.log(' empty():', empty(), '(expected: undefined)'); 44 + 45 + // Test 7: Many parameters 46 + console.log('\nTest 7: Function with three parameters'); 47 + let add3 = new Function('a', 'b', 'c', 'return a + b + c'); 48 + console.log(' add3(1, 2, 3):', add3(1, 2, 3), '(expected: 6)'); 49 + 50 + // Test 8: String concatenation 51 + console.log('\nTest 8: String concatenation'); 52 + let greet = new Function('name', 'return "Hello, " + name + "!"'); 53 + console.log(' greet("World"):', greet("World"), '(expected: "Hello, World!")'); 54 + 55 + console.log('\n=== All Function Constructor Tests Complete ===');