2012-04-27 16:26:15 +00:00
|
|
|
|
/*
|
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 11:33:49 +00:00
|
|
|
|
| PHP Version 7 |
|
2012-04-27 16:26:15 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-10 22:08:42 +00:00
|
|
|
|
| Copyright (c) 1997-2017 The PHP Group |
|
2012-04-27 16:26:15 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
2012-04-27 16:26:15 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
2012-04-27 16:41:32 +00:00
|
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2017-03-11 09:17:10 +00:00
|
|
|
|
| Author: Stefan Siegl <stesie@php.net> |
|
2012-04-27 16:26:15 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
|
#include "php_v8js_macros.h"
|
2015-08-01 15:09:48 +00:00
|
|
|
|
#include "v8js_commonjs.h"
|
2017-03-10 22:08:42 +00:00
|
|
|
|
#include "v8js_exceptions.h"
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
2012-04-27 16:26:15 +00:00
|
|
|
|
extern "C" {
|
2013-03-24 22:53:31 +00:00
|
|
|
|
#include "zend_exceptions.h"
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* global.exit - terminate execution */
|
|
|
|
|
V8JS_METHOD(exit) /* {{{ */
|
|
|
|
|
{
|
2015-05-02 18:36:55 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2015-08-21 13:55:52 +00:00
|
|
|
|
v8js_terminate_execution(isolate);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/* global.sleep - sleep for passed seconds */
|
|
|
|
|
V8JS_METHOD(sleep) /* {{{ */
|
|
|
|
|
{
|
2013-07-07 22:58:19 +00:00
|
|
|
|
php_sleep(info[0]->Int32Value());
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/* global.print - php print() */
|
|
|
|
|
V8JS_METHOD(print) /* {{{ */
|
|
|
|
|
{
|
2013-10-25 18:16:02 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2017-03-10 22:08:42 +00:00
|
|
|
|
zend_long ret = 0;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
for (int i = 0; i < info.Length(); i++) {
|
|
|
|
|
v8::String::Utf8Value str(info[i]);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
const char *cstr = ToCString(str);
|
|
|
|
|
ret = PHPWRITE(cstr, strlen(cstr));
|
|
|
|
|
}
|
2017-03-10 22:08:42 +00:00
|
|
|
|
|
|
|
|
|
info.GetReturnValue().Set(zend_long_to_v8js(ret, isolate));
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
|
static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int level) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (level > 1) {
|
|
|
|
|
php_printf("%*c", (level - 1) * 2, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (var.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
php_printf("<empty>\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (var->IsNull() || var->IsUndefined() /* PHP compat */)
|
2013-10-20 15:39:30 +00:00
|
|
|
|
{
|
|
|
|
|
php_printf("NULL\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (var->IsInt32())
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
php_printf("int(%ld)\n", (long) var->IntegerValue());
|
|
|
|
|
return;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (var->IsUint32())
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
php_printf("int(%lu)\n", (unsigned long) var->IntegerValue());
|
|
|
|
|
return;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (var->IsNumber())
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
php_printf("float(%f)\n", var->NumberValue());
|
|
|
|
|
return;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (var->IsBoolean())
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
php_printf("bool(%s)\n", var->BooleanValue() ? "true" : "false");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 20:50:00 +00:00
|
|
|
|
v8::TryCatch try_catch(isolate); /* object.toString() can throw an exception */
|
2015-12-30 13:59:37 +00:00
|
|
|
|
v8::Local<v8::String> details;
|
|
|
|
|
|
|
|
|
|
if(var->IsRegExp()) {
|
|
|
|
|
v8::RegExp *re = v8::RegExp::Cast(*var);
|
|
|
|
|
details = re->GetSource();
|
2013-10-22 14:18:04 +00:00
|
|
|
|
}
|
2015-12-30 13:59:37 +00:00
|
|
|
|
else {
|
2017-04-23 20:50:00 +00:00
|
|
|
|
details = var->ToDetailString(isolate->GetEnteredContext()).FromMaybe(v8::Local<v8::String>());
|
2015-12-30 13:59:37 +00:00
|
|
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
|
details = V8JS_SYM("<toString threw exception>");
|
|
|
|
|
}
|
2013-10-22 14:18:04 +00:00
|
|
|
|
}
|
2015-12-30 13:59:37 +00:00
|
|
|
|
|
2013-10-22 14:18:04 +00:00
|
|
|
|
v8::String::Utf8Value str(details);
|
|
|
|
|
const char *valstr = ToCString(str);
|
2014-03-22 23:28:53 +00:00
|
|
|
|
size_t valstr_len = details->ToString()->Utf8Length();
|
2013-10-22 14:18:04 +00:00
|
|
|
|
|
|
|
|
|
if (var->IsString())
|
|
|
|
|
{
|
2016-03-26 11:11:11 +00:00
|
|
|
|
php_printf("string(%zu) \"", valstr_len);
|
2014-03-22 23:54:15 +00:00
|
|
|
|
PHPWRITE(valstr, valstr_len);
|
2014-03-22 23:28:53 +00:00
|
|
|
|
php_printf("\"\n");
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
else if (var->IsDate())
|
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
// fake the fields of a PHP DateTime
|
2012-04-27 16:26:15 +00:00
|
|
|
|
php_printf("Date(%s)\n", valstr);
|
|
|
|
|
}
|
|
|
|
|
else if (var->IsRegExp())
|
|
|
|
|
{
|
2015-12-30 13:59:37 +00:00
|
|
|
|
php_printf("regexp(/%s/)\n", valstr);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
else if (var->IsArray())
|
|
|
|
|
{
|
|
|
|
|
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(var);
|
|
|
|
|
uint32_t length = array->Length();
|
|
|
|
|
|
|
|
|
|
php_printf("array(%d) {\n", length);
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
|
php_printf("%*c[%d] =>\n", level * 2, ' ', i);
|
2017-03-11 12:35:21 +00:00
|
|
|
|
v8js_dumper(isolate, array->Get(i), level + 1);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (level > 1) {
|
|
|
|
|
php_printf("%*c", (level - 1) * 2, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ZEND_PUTS("}\n");
|
|
|
|
|
}
|
|
|
|
|
else if (var->IsObject())
|
|
|
|
|
{
|
|
|
|
|
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(var);
|
|
|
|
|
V8JS_GET_CLASS_NAME(cname, object);
|
2013-10-22 14:18:04 +00:00
|
|
|
|
int hash = object->GetIdentityHash();
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
2015-11-28 14:18:38 +00:00
|
|
|
|
if (var->IsFunction() && strcmp(ToCString(cname), "Closure") != 0)
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
|
|
|
|
v8::String::Utf8Value csource(object->ToString());
|
2013-10-22 14:18:04 +00:00
|
|
|
|
php_printf("object(Closure)#%d {\n%*c%s\n", hash, level * 2 + 2, ' ', ToCString(csource));
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-10-22 14:18:04 +00:00
|
|
|
|
v8::Local<v8::Array> keys = object->GetOwnPropertyNames();
|
2012-04-27 16:26:15 +00:00
|
|
|
|
uint32_t length = keys->Length();
|
|
|
|
|
|
2013-10-22 14:18:04 +00:00
|
|
|
|
if (strcmp(ToCString(cname), "Array") == 0 ||
|
|
|
|
|
strcmp(ToCString(cname), "V8Object") == 0) {
|
|
|
|
|
php_printf("array");
|
|
|
|
|
} else {
|
|
|
|
|
php_printf("object(%s)#%d", ToCString(cname), hash);
|
|
|
|
|
}
|
|
|
|
|
php_printf(" (%d) {\n", length);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
|
v8::Local<v8::String> key = keys->Get(i)->ToString();
|
|
|
|
|
v8::String::Utf8Value kname(key);
|
|
|
|
|
php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
|
2017-03-11 12:35:21 +00:00
|
|
|
|
v8js_dumper(isolate, object->Get(key), level + 1);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (level > 1) {
|
|
|
|
|
php_printf("%*c", (level - 1) * 2, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ZEND_PUTS("}\n");
|
|
|
|
|
}
|
|
|
|
|
else /* null, undefined, etc. */
|
|
|
|
|
{
|
|
|
|
|
php_printf("<%s>\n", valstr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/* global.var_dump - Dump JS values */
|
|
|
|
|
V8JS_METHOD(var_dump) /* {{{ */
|
|
|
|
|
{
|
2013-10-25 18:16:02 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
for (int i = 0; i < info.Length(); i++) {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
v8js_dumper(isolate, info[i], 1);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
info.GetReturnValue().Set(V8JS_NULL);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
|
V8JS_METHOD(require)
|
2013-03-24 22:27:46 +00:00
|
|
|
|
{
|
2013-10-25 18:16:02 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8js_ctx *c = (v8js_ctx *) isolate->GetData(0);
|
2013-10-19 20:11:17 +00:00
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8::String::Utf8Value module_base(info.Data());
|
|
|
|
|
const char *module_base_cstr = ToCString(module_base);
|
2013-03-24 22:27:46 +00:00
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
|
// Check that we have a module loader
|
2015-08-28 19:05:16 +00:00
|
|
|
|
if(Z_TYPE(c->module_loader) == IS_NULL) {
|
2013-12-21 00:18:08 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("No module loader")));
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 22:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
v8::String::Utf8Value module_id_v8(info[0]);
|
2015-08-01 17:45:21 +00:00
|
|
|
|
const char *module_id = ToCString(module_id_v8);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
char *normalised_path, *module_name;
|
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
if (Z_TYPE(c->module_normaliser) == IS_NULL) {
|
2015-12-06 12:55:13 +00:00
|
|
|
|
// No custom normalisation routine registered, use internal one
|
|
|
|
|
normalised_path = (char *)emalloc(PATH_MAX);
|
|
|
|
|
module_name = (char *)emalloc(PATH_MAX);
|
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8js_commonjs_normalise_identifier(module_base_cstr, module_id, normalised_path, module_name);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Call custom normaliser
|
|
|
|
|
int call_result;
|
2015-12-31 19:25:24 +00:00
|
|
|
|
zval params[2];
|
|
|
|
|
zval normaliser_result;
|
2015-12-06 12:55:13 +00:00
|
|
|
|
|
|
|
|
|
zend_try {
|
|
|
|
|
{
|
|
|
|
|
isolate->Exit();
|
|
|
|
|
v8::Unlocker unlocker(isolate);
|
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
ZVAL_STRING(¶ms[0], module_base_cstr);
|
2015-12-31 19:25:24 +00:00
|
|
|
|
ZVAL_STRING(¶ms[1], module_id);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
call_result = call_user_function_ex(EG(function_table), NULL, &c->module_normaliser,
|
2017-03-11 12:35:21 +00:00
|
|
|
|
&normaliser_result, 2, params, 0, NULL);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isolate->Enter();
|
|
|
|
|
|
|
|
|
|
if (call_result == FAILURE) {
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module normaliser callback failed")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
zend_catch {
|
|
|
|
|
v8js_terminate_execution(isolate);
|
|
|
|
|
V8JSG(fatal_error_abort) = 1;
|
|
|
|
|
call_result = FAILURE;
|
|
|
|
|
}
|
|
|
|
|
zend_end_try();
|
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(¶ms[0]);
|
|
|
|
|
zval_ptr_dtor(¶ms[1]);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
|
|
|
|
|
if(call_result == FAILURE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-03-25 00:02:13 +00:00
|
|
|
|
|
2015-12-06 12:55:13 +00:00
|
|
|
|
// Check if an exception was thrown
|
|
|
|
|
if (EG(exception)) {
|
2016-07-03 20:57:11 +00:00
|
|
|
|
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
|
|
|
|
|
zval tmp_zv;
|
|
|
|
|
ZVAL_OBJ(&tmp_zv, EG(exception));
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
|
|
|
|
|
zend_clear_exception();
|
|
|
|
|
} else {
|
|
|
|
|
v8js_terminate_execution(isolate);
|
|
|
|
|
}
|
2015-12-06 12:55:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2013-03-25 00:02:13 +00:00
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
if (Z_TYPE(normaliser_result) != IS_ARRAY) {
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&normaliser_result);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module normaliser didn't return an array")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
HashTable *ht = HASH_OF(&normaliser_result);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
int num_elements = zend_hash_num_elements(ht);
|
|
|
|
|
|
|
|
|
|
if(num_elements != 2) {
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&normaliser_result);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module normaliser expected to return array of 2 strings")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
zval *data;
|
2015-12-06 12:55:13 +00:00
|
|
|
|
ulong index = 0;
|
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
|
ZEND_HASH_FOREACH_VAL(ht, data) {
|
|
|
|
|
if (Z_TYPE_P(data) != IS_STRING) {
|
|
|
|
|
convert_to_string(data);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(index++) {
|
|
|
|
|
case 0: // normalised path
|
2015-12-31 19:25:24 +00:00
|
|
|
|
normalised_path = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
|
2015-12-06 12:55:13 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1: // normalised module id
|
2015-12-31 19:25:24 +00:00
|
|
|
|
module_name = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
|
2015-12-06 12:55:13 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-31 19:25:24 +00:00
|
|
|
|
ZEND_HASH_FOREACH_END();
|
2015-12-06 12:55:13 +00:00
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&normaliser_result);
|
2015-12-06 12:55:13 +00:00
|
|
|
|
}
|
2013-03-24 22:27:46 +00:00
|
|
|
|
|
2013-10-25 18:16:02 +00:00
|
|
|
|
char *normalised_module_id = (char *)emalloc(strlen(normalised_path)+1+strlen(module_name)+1);
|
2013-03-25 00:02:13 +00:00
|
|
|
|
*normalised_module_id = 0;
|
|
|
|
|
|
|
|
|
|
if (strlen(normalised_path) > 0) {
|
|
|
|
|
strcat(normalised_module_id, normalised_path);
|
|
|
|
|
strcat(normalised_module_id, "/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strcat(normalised_module_id, module_name);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
efree(module_name);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2013-03-24 19:08:54 +00:00
|
|
|
|
// Check for module cyclic dependencies
|
2013-04-12 23:00:27 +00:00
|
|
|
|
for (std::vector<char *>::iterator it = c->modules_stack.begin(); it != c->modules_stack.end(); ++it)
|
2013-03-24 19:08:54 +00:00
|
|
|
|
{
|
2013-03-25 00:02:13 +00:00
|
|
|
|
if (!strcmp(*it, normalised_module_id)) {
|
2013-10-26 05:20:41 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
efree(normalised_path);
|
|
|
|
|
|
2013-12-21 00:18:08 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module cyclic dependency")));
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 19:08:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have already loaded and cached this module then use it
|
2014-09-19 22:36:22 +00:00
|
|
|
|
if (c->modules_loaded.count(normalised_module_id) > 0) {
|
2017-11-12 15:25:59 +00:00
|
|
|
|
v8::Persistent<v8::Value> newobj;
|
2014-09-19 22:36:22 +00:00
|
|
|
|
newobj.Reset(isolate, c->modules_loaded[normalised_module_id]);
|
|
|
|
|
info.GetReturnValue().Set(newobj);
|
2015-08-01 16:11:39 +00:00
|
|
|
|
|
|
|
|
|
efree(normalised_module_id);
|
|
|
|
|
efree(normalised_path);
|
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 19:08:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Callback to PHP to load the module code
|
|
|
|
|
|
2015-08-28 19:05:16 +00:00
|
|
|
|
zval module_code;
|
2015-12-04 21:09:04 +00:00
|
|
|
|
int call_result;
|
2015-08-28 19:05:16 +00:00
|
|
|
|
zval params[1];
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2015-12-31 22:42:01 +00:00
|
|
|
|
{
|
|
|
|
|
isolate->Exit();
|
|
|
|
|
v8::Unlocker unlocker(isolate);
|
2015-12-04 21:09:04 +00:00
|
|
|
|
|
2015-12-31 22:42:01 +00:00
|
|
|
|
zend_try {
|
2015-12-31 19:25:24 +00:00
|
|
|
|
ZVAL_STRING(¶ms[0], normalised_module_id);
|
2017-03-11 12:35:21 +00:00
|
|
|
|
call_result = call_user_function_ex(EG(function_table), NULL, &c->module_loader, &module_code, 1, params, 0, NULL);
|
2015-12-04 21:09:04 +00:00
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
zend_catch {
|
|
|
|
|
v8js_terminate_execution(isolate);
|
|
|
|
|
V8JSG(fatal_error_abort) = 1;
|
2015-12-04 21:09:04 +00:00
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
zend_end_try();
|
2015-12-04 21:09:04 +00:00
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
|
|
|
|
|
isolate->Enter();
|
|
|
|
|
|
|
|
|
|
if (V8JSG(fatal_error_abort)) {
|
2015-12-04 21:09:04 +00:00
|
|
|
|
call_result = FAILURE;
|
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
else if (call_result == FAILURE) {
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module loader callback failed")));
|
|
|
|
|
}
|
2015-12-04 21:09:04 +00:00
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(¶ms[0]);
|
2015-12-31 19:25:24 +00:00
|
|
|
|
|
2015-12-04 21:09:04 +00:00
|
|
|
|
if (call_result == FAILURE) {
|
2013-10-26 05:20:41 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
efree(normalised_path);
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 16:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 22:53:31 +00:00
|
|
|
|
// Check if an exception was thrown
|
|
|
|
|
if (EG(exception)) {
|
2013-10-26 05:20:41 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
efree(normalised_path);
|
|
|
|
|
|
2016-07-03 20:52:50 +00:00
|
|
|
|
if (c->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) {
|
|
|
|
|
zval tmp_zv;
|
|
|
|
|
ZVAL_OBJ(&tmp_zv, EG(exception));
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(zval_to_v8js(&tmp_zv, isolate)));
|
|
|
|
|
zend_clear_exception();
|
|
|
|
|
} else {
|
|
|
|
|
v8js_terminate_execution(isolate);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 22:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 20:44:40 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 22:27:46 +00:00
|
|
|
|
// Convert the return value to string
|
2015-08-28 19:05:16 +00:00
|
|
|
|
if (Z_TYPE(module_code) != IS_STRING) {
|
|
|
|
|
convert_to_string(&module_code);
|
2013-03-24 22:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 15:14:32 +00:00
|
|
|
|
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, c->context);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
|
|
|
|
// Catch JS exceptions
|
2017-04-23 20:50:00 +00:00
|
|
|
|
v8::TryCatch try_catch(isolate);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2013-10-25 18:16:02 +00:00
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2014-09-19 22:36:22 +00:00
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
|
// Enter the module context
|
|
|
|
|
v8::Context::Scope scope(context);
|
2017-11-12 15:14:32 +00:00
|
|
|
|
|
2013-03-24 16:27:13 +00:00
|
|
|
|
// Set script identifier
|
2015-12-06 12:23:14 +00:00
|
|
|
|
v8::Local<v8::String> sname = V8JS_STR(normalised_module_id);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2017-03-10 22:08:42 +00:00
|
|
|
|
if (Z_STRLEN(module_code) > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Module code size exceeds maximum supported length", 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 15:14:32 +00:00
|
|
|
|
v8::Local<v8::String> source = V8JS_ZSTR(Z_STR(module_code));
|
2013-10-26 05:20:41 +00:00
|
|
|
|
zval_ptr_dtor(&module_code);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
source = v8::String::Concat(V8JS_SYM("(function (exports, module, require) {"), source);
|
2017-11-12 15:14:32 +00:00
|
|
|
|
source = v8::String::Concat(source, V8JS_SYM("\n});"));
|
|
|
|
|
|
2013-03-24 16:27:13 +00:00
|
|
|
|
// Create and compile script
|
2014-03-22 16:58:38 +00:00
|
|
|
|
v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
|
|
|
|
// The script will be empty if there are compile errors
|
|
|
|
|
if (script.IsEmpty()) {
|
2013-10-26 05:20:41 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
efree(normalised_path);
|
2013-12-21 00:18:08 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module script compile failed")));
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 16:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8::Local<v8::String> base_path = V8JS_STR(normalised_path);
|
|
|
|
|
v8::MaybeLocal<v8::Function> require_fn = v8::FunctionTemplate::New(isolate, V8JS_MN(require), base_path)->GetFunction();
|
|
|
|
|
|
|
|
|
|
if (require_fn.IsEmpty()) {
|
|
|
|
|
efree(normalised_path);
|
|
|
|
|
efree(normalised_module_id);
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Failed to create require method")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-03-25 00:02:13 +00:00
|
|
|
|
// Add this module and path to the stack
|
2013-04-12 23:00:27 +00:00
|
|
|
|
c->modules_stack.push_back(normalised_module_id);
|
2013-03-24 19:08:54 +00:00
|
|
|
|
|
2017-11-12 15:14:32 +00:00
|
|
|
|
// Run script to evaluate closure
|
|
|
|
|
v8::Local<v8::Value> module_function = script->Run();
|
|
|
|
|
|
|
|
|
|
// Prepare exports & module object
|
|
|
|
|
v8::Local<v8::Object> exports = v8::Object::New(isolate);
|
|
|
|
|
|
|
|
|
|
v8::Local<v8::Object> module = v8::Object::New(isolate);
|
|
|
|
|
module->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
|
|
|
|
|
module->Set(V8JS_SYM("exports"), exports);
|
|
|
|
|
|
|
|
|
|
if (module_function->IsFunction()) {
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(3 * sizeof(v8::Local<v8::Value>)));
|
2017-11-12 15:14:32 +00:00
|
|
|
|
new(&jsArgv[0]) v8::Local<v8::Value>;
|
|
|
|
|
jsArgv[0] = exports;
|
|
|
|
|
|
|
|
|
|
new(&jsArgv[1]) v8::Local<v8::Value>;
|
|
|
|
|
jsArgv[1] = module;
|
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
new(&jsArgv[2]) v8::Local<v8::Value>;
|
|
|
|
|
jsArgv[2] = require_fn.ToLocalChecked();
|
|
|
|
|
|
2017-11-12 15:14:32 +00:00
|
|
|
|
// actually call the module
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8::Local<v8::Function>::Cast(module_function)->Call(exports, 3, jsArgv);
|
2017-11-12 15:14:32 +00:00
|
|
|
|
}
|
2013-03-24 19:08:54 +00:00
|
|
|
|
|
2013-03-25 00:02:13 +00:00
|
|
|
|
// Remove this module and path from the stack
|
2013-04-12 23:00:27 +00:00
|
|
|
|
c->modules_stack.pop_back();
|
|
|
|
|
|
|
|
|
|
efree(normalised_path);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2017-11-12 15:14:32 +00:00
|
|
|
|
if (!module_function->IsFunction()) {
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Wrapped module script failed to return function")));
|
|
|
|
|
efree(normalised_module_id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 16:27:13 +00:00
|
|
|
|
// Script possibly terminated, return immediately
|
|
|
|
|
if (!try_catch.CanContinue()) {
|
2013-12-21 00:18:08 +00:00
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module script compile failed")));
|
2014-09-19 22:36:22 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 16:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle runtime JS exceptions
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
|
// Rethrow the exception back to JS
|
2013-07-07 22:58:19 +00:00
|
|
|
|
info.GetReturnValue().Set(try_catch.ReThrow());
|
2014-09-19 22:36:22 +00:00
|
|
|
|
efree(normalised_module_id);
|
2013-07-07 22:58:19 +00:00
|
|
|
|
return;
|
2013-03-24 16:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 15:25:59 +00:00
|
|
|
|
v8::Local<v8::Value> newobj;
|
2014-09-19 22:36:22 +00:00
|
|
|
|
|
2013-03-24 19:08:54 +00:00
|
|
|
|
// Cache the module so it doesn't need to be compiled and run again
|
2013-03-26 00:09:21 +00:00
|
|
|
|
// Ensure compatibility with CommonJS implementations such as NodeJS by playing nicely with module.exports and exports
|
2017-11-12 15:14:32 +00:00
|
|
|
|
if (module->Has(V8JS_SYM("exports"))) {
|
2017-11-12 15:25:59 +00:00
|
|
|
|
newobj = module->Get(V8JS_SYM("exports"));
|
2013-03-26 00:09:21 +00:00
|
|
|
|
}
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2014-09-19 22:36:22 +00:00
|
|
|
|
c->modules_loaded[normalised_module_id].Reset(isolate, newobj);
|
|
|
|
|
info.GetReturnValue().Set(newobj);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 12:31:40 +00:00
|
|
|
|
void v8js_register_methods(v8::Local<v8::ObjectTemplate> global, v8js_ctx *c) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
|
{
|
2013-10-25 18:16:02 +00:00
|
|
|
|
v8::Isolate *isolate = c->isolate;
|
2014-10-19 11:58:21 +00:00
|
|
|
|
global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(isolate, V8JS_MN(exit)), v8::ReadOnly);
|
|
|
|
|
global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(isolate, V8JS_MN(sleep)), v8::ReadOnly);
|
|
|
|
|
global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(isolate, V8JS_MN(print)), v8::ReadOnly);
|
|
|
|
|
global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(isolate, V8JS_MN(var_dump)), v8::ReadOnly);
|
2013-03-24 16:27:13 +00:00
|
|
|
|
|
2018-02-01 18:38:47 +00:00
|
|
|
|
v8::Local<v8::String> base_path = V8JS_STRL("", 0);
|
|
|
|
|
global->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(isolate, V8JS_MN(require), base_path), v8::ReadOnly);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* tab-width: 4
|
|
|
|
|
* c-basic-offset: 4
|
2016-07-02 20:41:01 +00:00
|
|
|
|
* indent-tabs-mode: t
|
2012-04-27 16:26:15 +00:00
|
|
|
|
* End:
|
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
|
*/
|