Skip to content

Commit 3277169

Browse files
author
Martijn Otto
committed
Added automagic string casting for JS objects
1 parent b3cd191 commit 3277169

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

jsobject.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ Php::Value JSObject::__call(const char *name, Php::Parameters &params)
188188
return value(function->Call(static_cast<v8::Local<v8::Object>>(_object), input.size(), input.data()));
189189
}
190190

191+
/**
192+
* Cast to a string
193+
*
194+
* @return The result of the string conversion
195+
*/
196+
Php::Value JSObject::__toString()
197+
{
198+
// create a handle scope, so variables "fall out of scope" and "enter" the context
199+
v8::HandleScope scope(isolate());
200+
v8::Context::Scope context(_object->CreationContext());
201+
202+
// convert to string and then to php
203+
return value(_object->ToString());
204+
}
205+
191206
/**
192207
* Retrieve the iterator
193208
*

jsobject.h

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ class JSObject :
148148
*/
149149
Php::Value __call(const char *name, Php::Parameters &params);
150150

151+
/**
152+
* Cast to a string
153+
*
154+
* @return The result of the string conversion
155+
*/
156+
Php::Value __toString();
157+
151158
/**
152159
* Retrieve the iterator
153160
*

0 commit comments

Comments
 (0)