summaryrefslogtreecommitdiff
path: root/javascript
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2015-11-22 14:36:24 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2015-11-22 14:36:24 +0000
commit9fe01f09c879bd75c369ea064be519adaf3d9837 (patch)
tree7be11311aa9d8f179856c2b4a902c3fb5ce11073 /javascript
parentb41b672fe5228be936715254003132ee21843242 (diff)
downloadnetsurf-9fe01f09c879bd75c369ea064be519adaf3d9837.tar.gz
netsurf-9fe01f09c879bd75c369ea064be519adaf3d9837.tar.bz2
Replace duktape's default allocators. Realloc to zero is not guaranteed to free on all platforms
Diffstat (limited to 'javascript')
-rw-r--r--javascript/duktape/dukky.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/javascript/duktape/dukky.c b/javascript/duktape/dukky.c
index 3549cd5eb..e56d517ea 100644
--- a/javascript/duktape/dukky.c
+++ b/javascript/duktape/dukky.c
@@ -289,6 +289,41 @@ dukky_inject_not_ctr(duk_context *ctx, int idx, const char *name)
return;
}
+/* Duktape heap utility functions */
+
+/* We need to override the defaults because not all platforms are fully ANSI
+ * compatible. E.g. RISC OS gets upset if we malloc or realloc a zero byte
+ * block, as do debugging tools such as Electric Fence by Bruce Perens.
+ */
+
+static void *dukky_alloc_function(void *udata, duk_size_t size)
+{
+ if (size == 0)
+ return NULL;
+
+ return malloc(size);
+}
+
+static void *dukky_realloc_function(void *udata, void *ptr, duk_size_t size)
+{
+ if (ptr == NULL && size == 0)
+ return NULL;
+
+ if (size == 0) {
+ free(ptr);
+ return NULL;
+ }
+
+ return realloc(ptr, size);
+}
+
+static void dukky_free_function(void *udata, void *ptr)
+{
+ if (ptr != NULL)
+ free(ptr);
+}
+
+
/**************************************** js.h ******************************/
struct jscontext {
duk_context *ctx;
@@ -322,7 +357,12 @@ nserror js_newcontext(int timeout, jscallback *cb, void *cbctx,
*jsctx = NULL;
LOG("Creating new duktape javascript context");
if (ret == NULL) return NSERROR_NOMEM;
- ctx = ret->ctx = duk_create_heap_default();
+ ctx = ret->ctx = duk_create_heap(
+ dukky_alloc_function,
+ dukky_realloc_function,
+ dukky_free_function,
+ NULL,
+ NULL);
if (ret->ctx == NULL) { free(ret); return NSERROR_NOMEM; }
/* Create the prototype stuffs */
duk_push_global_object(ctx);