From 7663fb68882bf96241af3bc04b4afbfca38ec38e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 5 Jan 2018 21:44:40 +0100 Subject: [PATCH] support native modules --- tests/commonjs_native_basic.phpt | 24 ++++++++++++++++++++++++ v8js_methods.cc | 9 +++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/commonjs_native_basic.phpt diff --git a/tests/commonjs_native_basic.phpt b/tests/commonjs_native_basic.phpt new file mode 100644 index 0000000..b9e0e1e --- /dev/null +++ b/tests/commonjs_native_basic.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test V8Js::setModuleLoader : Native Module basic behaviour +--SKIPIF-- + +--FILE-- +setModuleLoader(function($module) { + return new NativeModule(); +}); +$v8->executeString('require("foo").sayHello("World");'); +?> +===EOF=== +--EXPECT-- +Hello World! +===EOF=== \ No newline at end of file diff --git a/v8js_methods.cc b/v8js_methods.cc index 980f0d7..947d348 100644 --- a/v8js_methods.cc +++ b/v8js_methods.cc @@ -406,6 +406,15 @@ V8JS_METHOD(require) return; } + if(Z_TYPE(module_code) == IS_OBJECT) { + v8::Local newobj = zval_to_v8js(&module_code, isolate)->ToObject(); + c->modules_loaded[normalised_module_id].Reset(isolate, newobj); + info.GetReturnValue().Set(newobj); + + efree(normalised_path); + return; + } + // Convert the return value to string if (Z_TYPE(module_code) != IS_STRING) { convert_to_string(&module_code);