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.

binding loss fixes

+69
+1
include/internal.h
··· 73 73 struct { 74 74 jsval_t func; 75 75 jsval_t closure_scope; 76 + jsval_t this_val; 76 77 const char *code_str; 77 78 jsoff_t fnlen; 78 79 jsval_t *args;
+42
src/ant.c
··· 6734 6734 } 6735 6735 6736 6736 static jsval_t append_spread_to_utarray(struct js *js, UT_array *args, jsval_t spread_value) { 6737 + if (vtype(spread_value) == T_ARR) { 6738 + jsoff_t len = js_arr_len(js, spread_value); 6739 + for (jsoff_t i = 0; i < len; i++) { 6740 + jsval_t elem = arr_get(js, spread_value, i); 6741 + utarray_push_back(args, &elem); 6742 + } return js_mkundef(); 6743 + } 6737 6744 spread_utarray_ctx_t ctx = { .args = args }; 6738 6745 return iter_foreach(js, spread_value, spread_utarray_iter_cb, &ctx); 6739 6746 } ··· 6765 6772 } 6766 6773 6767 6774 static jsval_t append_spread_to_dynargs(struct js *js, spread_dynargs_ctx_t *ctx, jsval_t spread_value) { 6775 + if (vtype(spread_value) == T_ARR) { 6776 + jsoff_t len = js_arr_len(js, spread_value); 6777 + for (jsoff_t i = 0; i < len; i++) spread_dynargs_push(ctx, arr_get(js, spread_value, i)); 6778 + return js_mkundef(); 6779 + } 6768 6780 return iter_foreach(js, spread_value, spread_dynargs_iter_cb, ctx); 6769 6781 } 6770 6782 ··· 7579 7591 7580 7592 jsval_t res; jsval_t *tc_args = NULL; 7581 7593 int tc_argc = 0; bool tc_iter = false; 7594 + bool tc_pushed_this = false; 7582 7595 7583 7596 for (;;) { 7584 7597 jsval_t target_this = peek_this(); ··· 7773 7786 fnlen = js->tc.fnlen; 7774 7787 closure_scope = js->tc.closure_scope; 7775 7788 func_val = js->tc.func; 7789 + if (tc_pushed_this) pop_this(); 7790 + push_this(js->tc.this_val); 7791 + tc_pushed_this = true; 7776 7792 bound_args = NULL; 7777 7793 bound_argc = 0; 7778 7794 tc_args = js->tc.args; ··· 7784 7800 7785 7801 break; 7786 7802 } // end trampoline 7803 + if (tc_pushed_this) pop_this(); 7787 7804 7788 7805 free(tc_args); 7789 7806 tc_args = NULL; ··· 8322 8339 js->tc.pending = true; 8323 8340 js->tc.func = func; 8324 8341 js->tc.closure_scope = closure_scope; 8342 + js->tc.this_val = peek_this(); 8325 8343 js->tc.code_str = code_str; 8326 8344 js->tc.fnlen = fnlen; 8327 8345 js->tc.args = tc_argv; ··· 9132 9150 } 9133 9151 9134 9152 static jsval_t append_spread_to_array_literal(struct js *js, jsval_t arr, jsoff_t *idx, jsval_t spread_value) { 9153 + uint8_t t = vtype(spread_value); 9154 + if (t == T_ARR) { 9155 + jsoff_t len = js_arr_len(js, spread_value); 9156 + for (jsoff_t i = 0; i < len; i++) { 9157 + arr_set(js, arr, *idx, arr_get(js, spread_value, i)); (*idx)++; 9158 + } 9159 + return js_mkundef(); 9160 + } 9161 + if (t == T_STR) { 9162 + jsoff_t len, off = vstr(js, spread_value, &len); 9163 + for (jsoff_t i = 0; i < len; ) { 9164 + unsigned char c = (unsigned char)js->mem[off + i]; 9165 + jsoff_t cb = 1; 9166 + if (c >= 0x80) { 9167 + int seq = utf8_sequence_length(c); 9168 + cb = seq > 0 ? (jsoff_t)seq : 1; 9169 + if (i + cb > len) cb = len - i; 9170 + } 9171 + arr_set(js, arr, *idx, js_mkstr(js, (char *)&js->mem[off + i], cb)); 9172 + (*idx)++; i += cb; 9173 + off = vstr(js, spread_value, &len); 9174 + } 9175 + return js_mkundef(); 9176 + } 9135 9177 spread_arr_literal_ctx_t ctx = { .arr = arr, .idx = idx }; 9136 9178 return iter_foreach(js, spread_value, spread_arr_literal_iter_cb, &ctx); 9137 9179 }
+26
tests/test_wrapped_this.js
··· 1 + class Foo { 2 + constructor() { this.x = 42; } 3 + bar() { return this.x; } 4 + } 5 + 6 + const foo = new Foo(); 7 + 8 + console.log("A:", foo.bar()); 9 + 10 + const f = foo; 11 + console.log("B:", f.bar()); 12 + 13 + function test1(obj) { return obj.bar(); } 14 + console.log("C:", test1(foo)); 15 + 16 + function test2(obj) { const o = obj; return o.bar(); } 17 + console.log("D:", test2(foo)); 18 + 19 + function test3(z) { return z.bar(); } 20 + console.log("E:", test3(foo)); 21 + 22 + function test4(obj) { return obj.x; } 23 + console.log("F:", test4(foo)); 24 + 25 + function test5(obj) { return obj.bar(); } 26 + console.log("G:", test5({x: 99, bar() { return this.x; }}));