Skip to content

Commit 6ae9e6e

Browse files
committed
Fix #52 - proper obj fixup
1 parent d6c33dd commit 6ae9e6e

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

elk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ static void js_fixup_offsets(struct js *js, jsoff_t start, jsoff_t size) {
327327
if (v & GCMASK) continue; // To be deleted, don't bother
328328
if ((v & 3) != T_OBJ && (v & 3) != T_PROP) continue;
329329
if (v > start) saveoff(js, off, v - size);
330+
331+
if ((v & 3) == T_OBJ) {
332+
jsoff_t u = loadoff(js, off + sizeof(jsoff_t));
333+
if (u > start) saveoff(js, off + sizeof(jsoff_t), u - size);
334+
}
335+
330336
if ((v & 3) == T_PROP) {
331337
jsoff_t koff = loadoff(js, off + sizeof(off));
332338
if (koff > start) saveoff(js, off + sizeof(off), koff - size);

test/unit_test.c

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -276,31 +276,6 @@ static void test_flow(void) {
276276
assert(ev(js, "a=0; if (0) a=1; else if (1) a=2; a;", "2"));
277277
assert(ev(js, "a=0; if (0){7;a=1;}else if (1){7;a=2;} a;", "2"));
278278
assert(ev(js, "a=0; if(0){7;a=1;}else if(0){5;a=2;}else{3;a=3;} a;", "3"));
279-
280-
#if 0
281-
{
282-
clock_t a = clock();
283-
const char *code =
284-
"a=0;b=''; let f = function(){return 'x';}; while (a++<99999)"
285-
"{b+=f(); if (b.length > 50) b='';} 42;";
286-
ev(js, code, "42");
287-
double ms = (double) (clock() - a) * 1000 / CLOCKS_PER_SEC;
288-
printf("done in %g ms\n", ms);
289-
}
290-
#endif
291-
292-
#if 0
293-
// Ternary operator
294-
assert(ev(js, "1?2:3", "2"));
295-
assert(ev(js, "0?2:3", "3"));
296-
assert(ev(js, "0?1+1:1+2", "3"));
297-
assert(ev(js, "a=0?1+1:1+2", "3"));
298-
assert(ev(js, "a", "3"));
299-
assert(ev(js, "a=b=0; a=b=0?1+1:1+2", "3"));
300-
assert(ev(js, "a=0?1+1:1+2; a++; a", "4"));
301-
assert(ev(js, "a=0; 0?a++:a--; a", "-1"));
302-
assert(ev(js, "a=1?2:0?3:4", "2"));
303-
#endif
304279
}
305280

306281
static void test_scopes(void) {
@@ -448,6 +423,11 @@ static void test_gc(void) {
448423
"let f=function(){let n=0; while (n++ < "
449424
"100){prnt(str(0,n)+'\\n');} return n;}; f()",
450425
"101"));
426+
427+
assert(ev(js,
428+
"let fn=function(x) {let i=0; while(i<x) {res=res+'x'; i++; } };"
429+
"let res=' 123 '; fn(3); res === ' 123 xxx'",
430+
"true"));
451431
}
452432

453433
static const char *hi(void) {
@@ -608,6 +588,18 @@ static void test_ternary(void) {
608588
assert(ev(js, "f(4)", "24"));
609589
assert(ev(js, "f(5)", "120"));
610590
assert(ev(js, "f(10)", "3628800"));
591+
592+
// Ternary operator
593+
assert(ev(js, "1?2:3", "2"));
594+
assert(ev(js, "0?2:3", "3"));
595+
assert(ev(js, "0?1+1:1+2", "3"));
596+
assert(ev(js, "let a,b=0?1+1:1+2; b", "3"));
597+
assert(ev(js, "a=b=0; a=b=0?1+1:1+2", "3"));
598+
#if 0
599+
assert(ev(js, "a=0?1+1:1+2; a", "3"));
600+
assert(ev(js, "a=0; 0?a++:a--; a", "-1"));
601+
assert(ev(js, "a=1?2:0?3:4", "2"));
602+
#endif
611603
}
612604

613605
int main(void) {

0 commit comments

Comments
 (0)