浏览代码

Use context specific allocator functions

bel2125 8 年之前
父节点
当前提交
98ef70cef8
共有 2 个文件被更改,包括 101 次插入93 次删除
  1. 96 88
      src/mod_duktape.inl
  2. 5 5
      src/mod_lua.inl

+ 96 - 88
src/mod_duktape.inl

@@ -22,14 +22,14 @@ static const char *civetweb_ctx_id = "\xFF"
 static void *
 mg_duk_mem_alloc(void *udata, duk_size_t size)
 {
-	return mg_malloc_ctx(size, ???);
+	return mg_malloc_ctx(size, (struct mg_context *)udata);
 }
 
 
 static void *
 mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
 {
-	return mg_realloc_ctx(ptr, newsize, ???);
+	return mg_realloc_ctx(ptr, newsize, (struct mg_context *)udata);
 }
 
 
@@ -41,40 +41,40 @@ mg_duk_mem_free(void *udata, void *ptr)
 
 
 static void
-mg_duk_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)
+mg_duk_fatal_handler(duk_context *duk_ctx, duk_errcode_t code, const char *msg)
 {
 	/* Script is called "protected" (duk_peval_file), so script errors should
 	 * never yield in a call to this function. Maybe calls prior to executing
 	 * the script could raise a fatal error. */
 	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);
+	duk_push_global_stash(duk_ctx);
+	duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
+	conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 
 	mg_cry(conn, "%s", msg);
 }
 
 
 static duk_ret_t
-duk_itf_write(duk_context *ctx)
+duk_itf_write(duk_context *duk_ctx)
 {
 	struct mg_connection *conn;
 	duk_double_t ret;
 	duk_size_t len = 0;
-	const char *val = duk_require_lstring(ctx, -1, &len);
+	const char *val = duk_require_lstring(duk_ctx, -1, &len);
 
 	/*
-	    duk_push_global_stash(ctx);
-	    duk_get_prop_string(ctx, -1, civetweb_conn_id);
-	    conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
+	    duk_push_global_stash(duk_ctx);
+	    duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
+	    conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 	*/
-	duk_push_current_function(ctx);
-	duk_get_prop_string(ctx, -1, civetweb_conn_id);
-	conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
+	duk_push_current_function(duk_ctx);
+	duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
+	conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 
 	if (!conn) {
-		duk_error(ctx,
+		duk_error(duk_ctx,
 		          DUK_ERR_INTERNAL_ERROR,
 		          "function not available without connection object");
 		/* probably never reached, but satisfies static code analysis */
@@ -83,24 +83,24 @@ duk_itf_write(duk_context *ctx)
 
 	ret = mg_write(conn, val, len);
 
-	duk_push_number(ctx, ret);
+	duk_push_number(duk_ctx, ret);
 	return 1;
 }
 
 
 static duk_ret_t
-duk_itf_read(duk_context *ctx)
+duk_itf_read(duk_context *duk_ctx)
 {
 	struct mg_connection *conn;
 	char buf[1024];
 	int len;
 
-	duk_push_global_stash(ctx);
-	duk_get_prop_string(ctx, -1, civetweb_conn_id);
-	conn = (struct mg_connection *)duk_to_pointer(ctx, -1);
+	duk_push_global_stash(duk_ctx);
+	duk_get_prop_string(duk_ctx, -1, civetweb_conn_id);
+	conn = (struct mg_connection *)duk_to_pointer(duk_ctx, -1);
 
 	if (!conn) {
-		duk_error(ctx,
+		duk_error(duk_ctx,
 		          DUK_ERR_INTERNAL_ERROR,
 		          "function not available without connection object");
 		/* probably never reached, but satisfies static code analysis */
@@ -109,25 +109,25 @@ duk_itf_read(duk_context *ctx)
 
 	len = mg_read(conn, buf, sizeof(buf));
 
-	duk_push_lstring(ctx, buf, len);
+	duk_push_lstring(duk_ctx, buf, len);
 	return 1;
 }
 
 
 static duk_ret_t
-duk_itf_getoption(duk_context *ctx)
+duk_itf_getoption(duk_context *duk_ctx)
 {
 	struct mg_context *cv_ctx;
 	const char *ret;
 	duk_size_t len = 0;
-	const char *val = duk_require_lstring(ctx, -1, &len);
+	const char *val = duk_require_lstring(duk_ctx, -1, &len);
 
-	duk_push_current_function(ctx);
-	duk_get_prop_string(ctx, -1, civetweb_ctx_id);
-	cv_ctx = (struct mg_context *)duk_to_pointer(ctx, -1);
+	duk_push_current_function(duk_ctx);
+	duk_get_prop_string(duk_ctx, -1, civetweb_ctx_id);
+	cv_ctx = (struct mg_context *)duk_to_pointer(duk_ctx, -1);
 
 	if (!cv_ctx) {
-		duk_error(ctx,
+		duk_error(duk_ctx,
 		          DUK_ERR_INTERNAL_ERROR,
 		          "function not available without connection object");
 		/* probably never reached, but satisfies static code analysis */
@@ -136,9 +136,9 @@ duk_itf_getoption(duk_context *ctx)
 
 	ret = mg_get_option(cv_ctx, val);
 	if (ret) {
-		duk_push_string(ctx, ret);
+		duk_push_string(duk_ctx, ret);
 	} else {
-		duk_push_null(ctx);
+		duk_push_null(duk_ctx);
 	}
 
 	return 1;
@@ -149,104 +149,112 @@ static void
 mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 {
 	int i;
-	duk_context *ctx = NULL;
+	duk_context *duk_ctx = NULL;
 
 	conn->must_close = 1;
 
 	/* Create Duktape interpreter state */
-	ctx = duk_create_heap(mg_duk_mem_alloc,
-	                      mg_duk_mem_realloc,
-	                      mg_duk_mem_free,
-	                      NULL,
-	                      mg_duk_fatal_handler);
-	if (!ctx) {
+	duk_ctx = duk_create_heap(mg_duk_mem_alloc,
+	                          mg_duk_mem_realloc,
+	                          mg_duk_mem_free,
+	                          (void *)conn->ctx,
+	                          mg_duk_fatal_handler);
+	if (!duk_ctx) {
 		mg_cry(conn, "Failed to create a Duktape heap.");
 		goto exec_duktape_finished;
 	}
 
 	/* Add "conn" object */
-	duk_push_global_object(ctx);
-	duk_push_object(ctx); /* create a new table/object ("conn") */
+	duk_push_global_object(duk_ctx);
+	duk_push_object(duk_ctx); /* create a new table/object ("conn") */
 
-	duk_push_c_function(ctx, duk_itf_write, 1 /* 1 = nargs */);
-	duk_push_pointer(ctx, (void *)conn);
-	duk_put_prop_string(ctx, -2, civetweb_conn_id);
-	duk_put_prop_string(ctx, -2, "write"); /* add function conn.write */
+	duk_push_c_function(duk_ctx, duk_itf_write, 1 /* 1 = nargs */);
+	duk_push_pointer(duk_ctx, (void *)conn);
+	duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
+	duk_put_prop_string(duk_ctx, -2, "write"); /* add function conn.write */
 
-	duk_push_c_function(ctx, duk_itf_read, 0 /* 0 = nargs */);
-	duk_push_pointer(ctx, (void *)conn);
-	duk_put_prop_string(ctx, -2, civetweb_conn_id);
-	duk_put_prop_string(ctx, -2, "read"); /* add function conn.read */
+	duk_push_c_function(duk_ctx, duk_itf_read, 0 /* 0 = nargs */);
+	duk_push_pointer(duk_ctx, (void *)conn);
+	duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
+	duk_put_prop_string(duk_ctx, -2, "read"); /* add function conn.read */
 
-	duk_push_string(ctx, conn->request_info.request_method);
-	duk_put_prop_string(ctx, -2, "request_method"); /* add string conn.r... */
+	duk_push_string(duk_ctx, conn->request_info.request_method);
+	duk_put_prop_string(duk_ctx,
+	                    -2,
+	                    "request_method"); /* add string conn.r... */
 
-	duk_push_string(ctx, conn->request_info.request_uri);
-	duk_put_prop_string(ctx, -2, "request_uri");
+	duk_push_string(duk_ctx, conn->request_info.request_uri);
+	duk_put_prop_string(duk_ctx, -2, "request_uri");
 
-	duk_push_string(ctx, conn->request_info.local_uri);
-	duk_put_prop_string(ctx, -2, "uri");
+	duk_push_string(duk_ctx, conn->request_info.local_uri);
+	duk_put_prop_string(duk_ctx, -2, "uri");
 
-	duk_push_string(ctx, conn->request_info.http_version);
-	duk_put_prop_string(ctx, -2, "http_version");
+	duk_push_string(duk_ctx, conn->request_info.http_version);
+	duk_put_prop_string(duk_ctx, -2, "http_version");
 
-	duk_push_string(ctx, conn->request_info.query_string);
-	duk_put_prop_string(ctx, -2, "query_string");
+	duk_push_string(duk_ctx, conn->request_info.query_string);
+	duk_put_prop_string(duk_ctx, -2, "query_string");
 
-	duk_push_string(ctx, conn->request_info.remote_addr);
-	duk_put_prop_string(ctx, -2, "remote_addr");
+	duk_push_string(duk_ctx, conn->request_info.remote_addr);
+	duk_put_prop_string(duk_ctx, -2, "remote_addr");
 
-	duk_push_int(ctx, conn->request_info.remote_port);
-	duk_put_prop_string(ctx, -2, "remote_port");
+	duk_push_int(duk_ctx, conn->request_info.remote_port);
+	duk_put_prop_string(duk_ctx, -2, "remote_port");
 
-	duk_push_int(ctx, ntohs(conn->client.lsa.sin.sin_port));
-	duk_put_prop_string(ctx, -2, "server_port");
+	duk_push_int(duk_ctx, ntohs(conn->client.lsa.sin.sin_port));
+	duk_put_prop_string(duk_ctx, -2, "server_port");
 
-	duk_push_object(ctx); /* subfolder "conn.http_headers" */
+	duk_push_object(duk_ctx); /* subfolder "conn.http_headers" */
 	for (i = 0; i < conn->request_info.num_headers; i++) {
-		duk_push_string(ctx, conn->request_info.http_headers[i].value);
-		duk_put_prop_string(ctx, -2, conn->request_info.http_headers[i].name);
+		duk_push_string(duk_ctx, conn->request_info.http_headers[i].value);
+		duk_put_prop_string(duk_ctx,
+		                    -2,
+		                    conn->request_info.http_headers[i].name);
 	}
-	duk_put_prop_string(ctx, -2, "http_headers");
+	duk_put_prop_string(duk_ctx, -2, "http_headers");
 
-	duk_put_prop_string(ctx, -2, "conn"); /* call the table "conn" */
+	duk_put_prop_string(duk_ctx, -2, "conn"); /* call the table "conn" */
 
 	/* Add "civetweb" object */
-	duk_push_global_object(ctx);
-	duk_push_object(ctx); /* create a new table/object ("conn") */
+	duk_push_global_object(duk_ctx);
+	duk_push_object(duk_ctx); /* create a new table/object ("conn") */
 
-	duk_push_string(ctx, CIVETWEB_VERSION);
-	duk_put_prop_string(ctx, -2, "version");
+	duk_push_string(duk_ctx, CIVETWEB_VERSION);
+	duk_put_prop_string(duk_ctx, -2, "version");
 
-	duk_push_string(ctx, script_name);
-	duk_put_prop_string(ctx, -2, "script_name");
+	duk_push_string(duk_ctx, script_name);
+	duk_put_prop_string(duk_ctx, -2, "script_name");
 
 	if (conn->ctx != NULL) {
-		duk_push_c_function(ctx, duk_itf_getoption, 1 /* 1 = nargs */);
-		duk_push_pointer(ctx, (void *)(conn->ctx));
-		duk_put_prop_string(ctx, -2, civetweb_ctx_id);
-		duk_put_prop_string(ctx, -2, "getoption"); /* add function conn.write */
+		duk_push_c_function(duk_ctx, duk_itf_getoption, 1 /* 1 = nargs */);
+		duk_push_pointer(duk_ctx, (void *)(conn->ctx));
+		duk_put_prop_string(duk_ctx, -2, civetweb_ctx_id);
+		duk_put_prop_string(duk_ctx,
+		                    -2,
+		                    "getoption"); /* add function conn.write */
 
 		if (conn->ctx->systemName != NULL) {
-			duk_push_string(ctx, conn->ctx->systemName);
-			duk_put_prop_string(ctx, -2, "system");
+			duk_push_string(duk_ctx, conn->ctx->systemName);
+			duk_put_prop_string(duk_ctx, -2, "system");
 		}
 	}
 
-	duk_put_prop_string(ctx, -2, "civetweb"); /* call the table "civetweb" */
+	duk_put_prop_string(duk_ctx,
+	                    -2,
+	                    "civetweb"); /* call the table "civetweb" */
 
-	duk_push_global_stash(ctx);
-	duk_push_pointer(ctx, (void *)conn);
-	duk_put_prop_string(ctx, -2, civetweb_conn_id);
+	duk_push_global_stash(duk_ctx);
+	duk_push_pointer(duk_ctx, (void *)conn);
+	duk_put_prop_string(duk_ctx, -2, civetweb_conn_id);
 
-	if (duk_peval_file(ctx, script_name) != 0) {
-		mg_cry(conn, "%s", duk_safe_to_string(ctx, -1));
+	if (duk_peval_file(duk_ctx, script_name) != 0) {
+		mg_cry(conn, "%s", duk_safe_to_string(duk_ctx, -1));
 		goto exec_duktape_finished;
 	}
-	duk_pop(ctx); /* ignore result */
+	duk_pop(duk_ctx); /* ignore result */
 
 exec_duktape_finished:
-	duk_destroy_heap(ctx);
+	duk_destroy_heap(duk_ctx);
 }
 
 

+ 5 - 5
src/mod_lua.inl

@@ -1614,14 +1614,13 @@ lua_error_handler(lua_State *L)
 static void *
 lua_allocator(void *ud, void *ptr, size_t osize, size_t nsize)
 {
-	(void)ud;
 	(void)osize; /* not used */
 
 	if (nsize == 0) {
 		mg_free(ptr);
 		return NULL;
 	}
-	return mg_realloc_ctx(ptr, nsize, ???);
+	return mg_realloc_ctx(ptr, nsize, (struct mg_context *)ud);
 }
 
 
@@ -1638,7 +1637,8 @@ mg_exec_lua_script(struct mg_connection *conn,
 	conn->must_close = 1;
 
 	/* Execute a plain Lua script. */
-	if (path != NULL && (L = lua_newstate(lua_allocator, NULL)) != NULL) {
+	if (path != NULL
+	    && (L = lua_newstate(lua_allocator, (void *)(conn->ctx))) != NULL) {
 		prepare_lua_environment(
 		    conn->ctx, conn, NULL, L, path, LUA_ENV_TYPE_PLAIN_LUA_PAGE);
 		lua_pushcclosure(L, &lua_error_handler, 0);
@@ -1733,7 +1733,7 @@ handle_lsp_request(struct mg_connection *conn,
 	if (ls != NULL) {
 		L = ls;
 	} else {
-		L = lua_newstate(lua_allocator, NULL);
+		L = lua_newstate(lua_allocator, (void *)(conn->ctx));
 		if (L == NULL) {
 			mg_send_http_error(
 			    conn,
@@ -1811,7 +1811,7 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 		ws->script = mg_strdup(script); /* TODO (low): handle OOM */
 		pthread_mutex_init(&(ws->ws_mutex), &pthread_mutex_attr);
 		(void)pthread_mutex_lock(&(ws->ws_mutex));
-		ws->state = lua_newstate(lua_allocator, NULL);
+		ws->state = lua_newstate(lua_allocator, (void *)(conn->ctx));
 		ws->conn[0] = conn;
 		ws->references = 1;
 		prepare_lua_environment(