Skip to content

Commit 21e07ed

Browse files
committed
Catch exceptions from PHP space when setting properties on object
1 parent 866c8fc commit 21e07ed

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

object.cpp

+29-11
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInf
216216
bool method_exists = Php::call("method_exists", *handle, *name);
217217
bool is_callable = handle->isCallable(*name);
218218
bool contains = handle->contains(*name, name.length());
219-
219+
220220
// does a property exist by the given name and is it not defined as a method?
221221
if (contains && !method_exists)
222222
{
@@ -270,8 +270,17 @@ static void setter(uint32_t index, v8::Local<v8::Value> input, const v8::Propert
270270
v8::HandleScope scope(Isolate::get());
271271
Handle handle(info.Holder()->GetInternalField(0));
272272

273-
// store the property inside the object
274-
handle->call("offsetSet", static_cast<int64_t>(index), value(input));
273+
// We are calling into PHP space so we need to catch all exceptions
274+
try
275+
{
276+
// store the property inside the object
277+
handle->call("offsetSet", static_cast<int64_t>(index), value(input));
278+
}
279+
catch (const Php::Exception& exception)
280+
{
281+
// pass the exception on to javascript userspace
282+
Isolate::get()->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(Isolate::get(), exception.what())));
283+
}
275284
}
276285

277286
/**
@@ -288,17 +297,26 @@ static void setter(v8::Local<v8::String> property, v8::Local<v8::Value> input, c
288297
Handle handle(info.Holder()->GetInternalField(0));
289298
v8::String::Utf8Value name(property);
290299

291-
// if the object is not implementing ArrayAccess or has the given property as a member
292-
// then we set it directly, otherwise we use the offsetSet method to use it as an array
293-
if (!handle->instanceOf("ArrayAccess") || handle->contains(*name, name.length()))
300+
// We are calling into PHP space so we need to catch all exceptions
301+
try
294302
{
295-
// store the property inside the object
296-
handle->set(*name, name.length(), value(input));
303+
// if the object is not implementing ArrayAccess or has the given property as a member
304+
// then we set it directly, otherwise we use the offsetSet method to use it as an array
305+
if (!handle->instanceOf("ArrayAccess") || handle->contains(*name, name.length()))
306+
{
307+
// store the property inside the object
308+
handle->set(*name, name.length(), value(input));
309+
}
310+
else
311+
{
312+
// set it as an array offset
313+
handle->call("offsetSet", Php::Value{ *name, name.length() }, value(input));
314+
}
297315
}
298-
else
316+
catch (const Php::Exception& exception)
299317
{
300-
// set it as an array offset
301-
handle->call("offsetSet", Php::Value{ *name, name.length() }, value(input));
318+
// pass the exception on to javascript userspace
319+
Isolate::get()->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(Isolate::get(), exception.what())));
302320
}
303321
}
304322

0 commit comments

Comments
 (0)