shithub: libmujs

Download patch

ref: ffc82eabd78042d4020776af64baf6a32de3a8d2
parent: 704d16409fcf5fde1724131fdffb85cb35331206
author: Tor Andersson <[email protected]>
date: Fri Jan 17 19:57:18 EST 2014

Add allocation counter to trigger garbage collector during interpretation.

Only trigger between function calls, during bytecode interpretation,
to allow for intra-function race conditions where an object has been
created but not yet inserted into the stack or environment scope chain.

--- a/jscompile.c
+++ b/jscompile.c
@@ -20,6 +20,7 @@
 	F->gcmark = 0;
 	F->gcnext = J->gcfun;
 	J->gcfun = F;
+	++J->gccounter;
 
 	F->filename = js_intern(J, J->filename);
 	F->line = name ? name->line : params ? params->line : body->line;
--- a/jsproperty.c
+++ b/jsproperty.c
@@ -136,6 +136,7 @@
 	obj->gcmark = 0;
 	obj->gcnext = J->gcobj;
 	J->gcobj = obj;
+	++J->gccounter;
 
 	obj->type = type;
 	obj->properties = &sentinel;
--- a/jsrun.c
+++ b/jsrun.c
@@ -327,6 +327,7 @@
 	E->gcmark = 0;
 	E->gcnext = J->gcenv;
 	J->gcenv = E;
+	++J->gccounter;
 
 	E->outer = outer;
 	E->variables = vars;
@@ -500,6 +501,11 @@
 	int b;
 
 	while (1) {
+		if (J->gccounter > JS_GCLIMIT) {
+			J->gccounter = 0;
+			js_gc(J, 0);
+		}
+
 		opcode = *pc++;
 		switch (opcode) {
 		case OP_POP: js_pop(J, 1); break;
--- a/jsstate.h
+++ b/jsstate.h
@@ -4,6 +4,7 @@
 #include "jsobject.h" /* for js_Value */
 
 #define JS_STACKSIZE 256
+#define JS_GCLIMIT 10000 /* run gc cycle every N allocations */
 
 struct js_State
 {
@@ -45,10 +46,12 @@
 
 	/* garbage collector list */
 	int gcmark;
+	int gccounter;
 	js_Environment *gcenv;
 	js_Function *gcfun;
 	js_Object *gcobj;
 
+	/* execution stack */
 	int top, bot;
 	js_Value stack[JS_STACKSIZE];
 };