Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 1d79ac3

Browse files
authored
Merge pull request #24 from pinepain/require_context
Improve exceptions handling
2 parents d1c251a + 7d94fe6 commit 1d79ac3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+212
-104
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ project(php_v8)
33

44
# NOTE: This CMake file is just for syntax highlighting in CLion
55

6-
include_directories(/usr/local/opt/v8@5.8/include)
7-
include_directories(/usr/local/opt/v8@5.8/include/libplatform)
6+
include_directories(/usr/local/opt/v8@5.9/include)
7+
include_directories(/usr/local/opt/v8@5.9/include/libplatform)
88

99
include_directories(/usr/local/include/php)
1010
include_directories(/usr/local/include/php/TSRM)

Vagrantfile

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
5+
VAGRANTFILE_API_VERSION = "2"
6+
7+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8+
config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" # Prevent TTY Errors
9+
10+
config.vm.box = "bento/ubuntu-16.04"
11+
12+
# config.vm.box_check_update = false
13+
14+
config.vm.network "private_network", ip: "192.168.33.44"
15+
# config.vm.network "public_network"
16+
17+
# If true, then any SSH connections made will enable agent forwarding.
18+
# Default value: false
19+
# config.ssh.forward_agent = true
20+
21+
config.vm.synced_folder ".", "/home/vagrant/php-v8"
22+
23+
# Provider-specific configuration so you can fine-tune various
24+
# backing providers for Vagrant. These expose provider-specific options.
25+
# Example for VirtualBox:
26+
#
27+
config.vm.provider "virtualbox" do |vb|
28+
# Don't boot with headless mode
29+
# vb.gui = true
30+
31+
# Use VBoxManage to customize the VM. For example to change memory:
32+
vb.customize ["modifyvm", :id, "--memory", 2048]
33+
vb.customize ["modifyvm", :id, "--cpus", 2]
34+
end
35+
#
36+
# View the documentation for the provider you're using for more
37+
# information on available options.
38+
39+
# Enable provisioning with CFEngine. CFEngine Community packages are
40+
# automatically installed. For example, configure the host as a
41+
# policy server and optionally a policy file to run:
42+
#
43+
# config.vm.provision "cfengine" do |cf|
44+
# cf.am_policy_hub = true
45+
# # cf.run_file = "motd.cf"
46+
# end
47+
#
48+
# You can also configure and bootstrap a client to an existing
49+
# policy server:
50+
#
51+
# config.vm.provision "cfengine" do |cf|
52+
# cf.policy_server_address = "10.0.2.15"
53+
# end
54+
55+
# Enable provisioning with Puppet stand alone. Puppet manifests
56+
# are contained in a directory path relative to this Vagrantfile.
57+
# You will need to create the manifests directory and a manifest in
58+
# the file default.pp in the manifests_path directory.
59+
#
60+
# config.vm.provision "puppet" do |puppet|
61+
# puppet.manifests_path = "manifests"
62+
# puppet.manifest_file = "default.pp"
63+
# end
64+
65+
# Enable provisioning with chef solo, specifying a cookbooks path, roles
66+
# path, and data_bags path (all relative to this Vagrantfile), and adding
67+
# some recipes and/or roles.
68+
#
69+
# config.vm.provision "chef_solo" do |chef|
70+
# chef.cookbooks_path = "../my-recipes/cookbooks"
71+
# chef.roles_path = "../my-recipes/roles"
72+
# chef.data_bags_path = "../my-recipes/data_bags"
73+
# chef.add_recipe "mysql"
74+
# chef.add_role "web"
75+
#
76+
# # You may also specify custom JSON attributes:
77+
# chef.json = { mysql_password: "foo" }
78+
# end
79+
80+
#config.vm.provision "shell", path: './provision/provision.sh', privileged: false
81+
end

src/php_v8_callbacks.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void php_v8_callback_call_from_bucket_with_zargs(size_t index, v8::Local<v8::Val
239239
phpv8::CallbacksBucket *bucket;
240240

241241
if (data.IsEmpty() || !data->IsExternal()) {
242-
PHP_V8_THROW_EXCEPTION("Callback has no stored callback function");
242+
PHP_V8_THROW_EXCEPTION("Callback doesn't have stored callback function");
243243
return;
244244
}
245245

@@ -249,7 +249,7 @@ void php_v8_callback_call_from_bucket_with_zargs(size_t index, v8::Local<v8::Val
249249

250250
// highly unlikely, but to play safe
251251
if (!cb) {
252-
PHP_V8_THROW_EXCEPTION("Callback has no stored callback function");
252+
PHP_V8_THROW_EXCEPTION("Callback doesn't have stored callback function");
253253
return;
254254
}
255255

src/php_v8_exceptions.cc

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
zend_class_entry* php_v8_generic_exception_class_entry;
2323
zend_class_entry* php_v8_try_catch_exception_class_entry;
2424
zend_class_entry* php_v8_termination_exception_class_entry;
25-
zend_class_entry* php_v8_abstract_resource_limit_exception_class_entry;
25+
zend_class_entry* php_v8_resource_limit_exception_class_entry;
2626
zend_class_entry* php_v8_time_limit_exception_class_entry;
2727
zend_class_entry* php_v8_memory_limit_exception_class_entry;
2828

@@ -190,7 +190,7 @@ static const zend_function_entry php_v8_termination_exception_methods[] = {
190190
PHP_FE_END
191191
};
192192

193-
static const zend_function_entry php_v8_abstract_resource_limit_exception_methods[] = {
193+
static const zend_function_entry php_v8_resource_limit_exception_methods[] = {
194194
PHP_FE_END
195195
};
196196

@@ -202,11 +202,6 @@ static const zend_function_entry php_v8_memory_limit_exception_methods[] = {
202202
PHP_FE_END
203203
};
204204

205-
static const zend_function_entry php_v8_script_exception_methods[] = {
206-
PHP_FE_END
207-
};
208-
209-
210205
static const zend_function_entry php_v8_value_exception_methods[] = {
211206
PHP_FE_END
212207
};
@@ -215,7 +210,7 @@ static const zend_function_entry php_v8_value_exception_methods[] = {
215210
PHP_MINIT_FUNCTION(php_v8_exceptions) {
216211
zend_class_entry ce;
217212

218-
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "GenericException", php_v8_exception_methods);
213+
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "Exception", php_v8_exception_methods);
219214
php_v8_generic_exception_class_entry = zend_register_internal_class_ex(&ce, zend_exception_get_default());
220215

221216
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "TryCatchException", php_v8_try_catch_exception_methods);
@@ -229,15 +224,14 @@ PHP_MINIT_FUNCTION(php_v8_exceptions) {
229224
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "TerminationException", php_v8_termination_exception_methods);
230225
php_v8_termination_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_try_catch_exception_class_entry);
231226

232-
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "AbstractResourceLimitException", php_v8_abstract_resource_limit_exception_methods);
233-
php_v8_abstract_resource_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_termination_exception_class_entry);
234-
php_v8_abstract_resource_limit_exception_class_entry->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
227+
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "ResourceLimitException", php_v8_resource_limit_exception_methods);
228+
php_v8_resource_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_termination_exception_class_entry);
235229

236230
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "TimeLimitException", php_v8_time_limit_exception_methods);
237-
php_v8_time_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_abstract_resource_limit_exception_class_entry);
231+
php_v8_time_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_resource_limit_exception_class_entry);
238232

239233
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "MemoryLimitException", php_v8_memory_limit_exception_methods);
240-
php_v8_memory_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_abstract_resource_limit_exception_class_entry);
234+
php_v8_memory_limit_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_resource_limit_exception_class_entry);
241235

242236
INIT_NS_CLASS_ENTRY(ce, "V8\\Exceptions", "ValueException", php_v8_value_exception_methods);
243237
php_v8_value_exception_class_entry = zend_register_internal_class_ex(&ce, php_v8_generic_exception_class_entry);

src/php_v8_exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern "C" {
3030
extern zend_class_entry* php_v8_generic_exception_class_entry;
3131
extern zend_class_entry* php_v8_try_catch_exception_class_entry;
3232
extern zend_class_entry* php_v8_termination_exception_class_entry;
33-
extern zend_class_entry* php_v8_abstract_resource_limit_exception_class_entry;
33+
extern zend_class_entry* php_v8_resource_limit_exception_class_entry;
3434
extern zend_class_entry* php_v8_time_limit_exception_class_entry;
3535
extern zend_class_entry* php_v8_memory_limit_exception_class_entry;
3636

src/php_v8_isolate.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,37 @@ static PHP_METHOD(V8Isolate, GetEnteredContext) {
363363
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
364364
PHP_V8_ENTER_ISOLATE(php_v8_isolate)
365365

366-
PHP_V8_ISOLATE_REQUIRE_IN_CONTEXT(isolate);
367-
368366
v8::Local<v8::Context> local_context = php_v8_isolate->isolate->GetEnteredContext();
369367

368+
if (local_context.IsEmpty()) {
369+
PHP_V8_THROW_EXCEPTION("Isolate doesn't have entered context");
370+
return;
371+
}
372+
370373
php_v8_context_t *php_v8_context = php_v8_context_get_reference(local_context);
371374

372375
ZVAL_OBJ(return_value, &php_v8_context->std);
373376
Z_ADDREF_P(return_value);
374377
}
375378

376379
static PHP_METHOD(V8Isolate, ThrowException) {
380+
zval *php_v8_context_zv;
377381
zval *php_v8_value_zv;
378382

379-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_value_zv) == FAILURE) {
383+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "oo", &php_v8_context_zv, &php_v8_value_zv) == FAILURE) {
380384
return;
381385
}
382386

383387
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
388+
389+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
384390
PHP_V8_VALUE_FETCH_WITH_CHECK(php_v8_value_zv, php_v8_value);
385391

392+
PHP_V8_DATA_ISOLATES_CHECK_USING(php_v8_context, php_v8_isolate);
386393
PHP_V8_DATA_ISOLATES_CHECK_USING(php_v8_value, php_v8_isolate);
387394

388-
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
389-
390-
PHP_V8_ISOLATE_REQUIRE_IN_CONTEXT(isolate);
395+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_context);
396+
PHP_V8_ENTER_CONTEXT(php_v8_context);
391397

392398
v8::Local<v8::Value> local_value = php_v8_value_get_local(php_v8_value);
393399
v8::Local<v8::Value> local_return_value = isolate->ThrowException(local_value);
@@ -549,7 +555,8 @@ ZEND_END_ARG_INFO()
549555
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_isolate_GetEnteredContext, ZEND_RETURN_VALUE, 0, V8\\Context, 0)
550556
ZEND_END_ARG_INFO()
551557

552-
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_isolate_ThrowException, ZEND_RETURN_VALUE, 1, V8\\Value, 0)
558+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_isolate_ThrowException, ZEND_RETURN_VALUE, 2, V8\\Value, 0)
559+
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
553560
ZEND_ARG_OBJ_INFO(0, value, V8\\Value, 0)
554561
ZEND_END_ARG_INFO()
555562

src/php_v8_script_origin.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ v8::ScriptOrigin *php_v8_create_script_origin_from_zval(zval *value, v8::Isolate
8383
v8::MaybeLocal<v8::String> local_resource_name = v8::String::NewFromUtf8(isolate, Z_STRVAL_P(resource_name_zv), v8::NewStringType::kNormal, (int)Z_STRLEN_P(resource_name_zv));
8484

8585
if (local_resource_name.IsEmpty()) {
86-
zend_throw_exception(php_v8_generic_exception_class_entry, "Invalid resource name", 0);
86+
PHP_V8_THROW_EXCEPTION("Invalid resource name");
8787
return nullptr;
8888
}
8989

@@ -113,7 +113,7 @@ v8::ScriptOrigin *php_v8_create_script_origin_from_zval(zval *value, v8::Isolate
113113
v8::MaybeLocal<v8::String> local_source_map_url = v8::String::NewFromUtf8(isolate, Z_STRVAL_P(source_map_url_zv), v8::NewStringType::kNormal, (int)Z_STRLEN_P(source_map_url_zv));
114114

115115
if (local_source_map_url.IsEmpty()) {
116-
zend_throw_exception(php_v8_generic_exception_class_entry, "Invalid source map url", 0);
116+
PHP_V8_THROW_EXCEPTION("Invalid source map url");
117117
return nullptr;
118118
}
119119

src/php_v8_stack_trace.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ static PHP_METHOD(V8StackTrace, GetFrame)
9999
frames = zend_read_property(this_ce, getThis(), ZEND_STRL("frames"), 0, &rv);
100100

101101
if (index < 0) {
102-
zend_throw_exception(php_v8_generic_exception_class_entry, "Fame index is out of range", 0);
102+
PHP_V8_THROW_EXCEPTION("Fame index is out of range");
103103
return;
104104
}
105105

106106
frame = zend_hash_index_find(Z_ARRVAL_P(frames), static_cast<zend_ulong>(index));
107107

108108
if (frame == NULL) {
109-
zend_throw_exception(php_v8_generic_exception_class_entry, "Fame index is out of range", 0);
109+
PHP_V8_THROW_EXCEPTION("Fame index is out of range");
110110
return;
111111
}
112112

src/php_v8_startup_data.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static PHP_METHOD(V8StartupData, __construct) {
6868
}
6969

7070
if (ZSTR_LEN(blob) > INT_MAX) {
71-
zend_throw_exception(php_v8_generic_exception_class_entry, "Failed to create startup blob due to blob size integer overflow", 0);
71+
PHP_V8_THROW_EXCEPTION("Failed to create startup blob due to blob size integer overflow");
7272
return;
7373
}
7474

stubs/src/Exceptions/GenericException.php renamed to stubs/src/Exceptions/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
namespace V8\Exceptions;
1717

1818

19-
class GenericException extends \Exception
19+
class Exception extends \Exception
2020
{
2121
}

0 commit comments

Comments
 (0)