فهرست منبع

support native modules

Stefan Siegl 7 سال پیش
والد
کامیت
7663fb6888
2فایلهای تغییر یافته به همراه33 افزوده شده و 0 حذف شده
  1. 24 0
      tests/commonjs_native_basic.phpt
  2. 9 0
      v8js_methods.cc

+ 24 - 0
tests/commonjs_native_basic.phpt

@@ -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===

+ 9 - 0
v8js_methods.cc

@@ -406,6 +406,15 @@ V8JS_METHOD(require)
 		return;
 		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
 	// Convert the return value to string
 	if (Z_TYPE(module_code) != IS_STRING) {
 	if (Z_TYPE(module_code) != IS_STRING) {
 		convert_to_string(&module_code);
 		convert_to_string(&module_code);