0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-22 03:01:51 +00:00

support native modules

This commit is contained in:
Stefan Siegl 2018-01-05 21:44:40 +01:00
parent ea89904966
commit 7663fb6888
No known key found for this signature in database
GPG Key ID: 73942AF5642F3DDA
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,24 @@
--TEST--
Test V8Js::setModuleLoader : Native Module basic behaviour
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
class NativeModule {
public function sayHello($name)
{
echo "Hello $name!\n";
}
}
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
return new NativeModule();
});
$v8->executeString('require("foo").sayHello("World");');
?>
===EOF===
--EXPECT--
Hello World!
===EOF===

View File

@ -406,6 +406,15 @@ V8JS_METHOD(require)
return;
}
if(Z_TYPE(module_code) == IS_OBJECT) {
v8::Local<v8::Object> 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);