Browse Source

Duktape: use mg_malloc/mg_realloc/mg_free

bel 9 years ago
parent
commit
8e7d98c8e7
3 changed files with 48 additions and 6 deletions
  1. 39 3
      src/mod_duktape.inl
  2. 2 3
      test/prime.ssjs
  3. 7 0
      test/syntax_error.ssjs

+ 39 - 3
src/mod_duktape.inl

@@ -5,8 +5,6 @@
 
 #include "duktape.h"
 
-/* TODO: Malloc function should use mg_malloc/mg_free */
-
 /* TODO: the mg context should be added to duktape as well */
 /* Alternative: redefine a new, clean API from scratch (instead of using mg),
  * or at least do not add problematic functions. */
@@ -19,6 +17,40 @@
 static const char *civetweb_conn_id = "civetweb_conn";
 
 
+static void *mg_duk_mem_alloc(void *udata, duk_size_t size)
+{
+	return mg_malloc(size);
+}
+
+
+static void *mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
+{
+	return mg_realloc(ptr, newsize);
+}
+
+
+static void mg_duk_mem_free(void *udata, void *ptr)
+{
+	mg_free(ptr);
+}
+
+
+static void
+mg_duk_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)
+{
+	/* TODO: check if this handler is required - duk_peval_file probably
+	 * already avoids that this function is called. */
+	/* TODO: test this handler (if it is called) */
+	struct mg_connection *conn;
+
+	duk_push_global_stash(ctx);
+	duk_get_prop_string(ctx, -1, civetweb_conn_id);
+	conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
+
+	mg_cry(conn, "%s", msg);
+}
+
+
 static duk_ret_t duk_itf_send(duk_context *ctx)
 {
 	struct mg_connection *conn;
@@ -51,7 +83,11 @@ static void mg_exec_duktape_script(struct mg_connection *conn, const char *path)
 
 	conn->must_close = 1;
 
-	ctx = duk_create_heap_default();
+	ctx = duk_create_heap(mg_duk_mem_alloc,
+	                      mg_duk_mem_realloc,
+	                      mg_duk_mem_free,
+	                      NULL,
+	                      mg_duk_fatal_handler);
 	if (!ctx) {
 		mg_cry(conn, "Failed to create a Duktape heap.");
 		goto exec_duktape_finished;

+ 2 - 3
test/prime.ssjs

@@ -1,4 +1,4 @@
-// prime.js - a test from the Duktape home page (with minimal modifications)
+// prime.js
 
 // Pure Ecmascript version of low level helper
 function primeCheckEcmascript(val, limit) {
@@ -22,7 +22,7 @@ function primeCheck(val) {
 function primeTest() {
     var res = [];
 
-    print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript) + '\n');
+    print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript));
     for (var i = 2; i <= 1000; i++) {
         if (primeCheck(i)) { res.push(i); }
     } 
@@ -34,4 +34,3 @@ print = this.send || print
 print('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n');
 
 primeTest();
-

+ 7 - 0
test/syntax_error.ssjs

@@ -0,0 +1,7 @@
+
+print('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n');
+
+print('Syntax error:');
+
+asdf ghjk qwert 123456789 +-*/
+.!,;