Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ option(PREFER_SYSTEM_LIBRARIES "Prefer to use system libraries over bundled." "O

option(DISABLE_DEBUG_MODE "Disable debugging mode." "OFF") # OFF for backward compatibility

option(SUPPORT_DUKTAPE "SUPPORT_DUKTAPE" ON)
option(SUPPORT_NODE "SUPPORT_NODE" ON)
option(SUPPORT_DUKTAPE "SUPPORT_DUKTAPE" OFF)
option(SUPPORT_NODE "SUPPORT_NODE" OFF)
option(SUPPORT_V8 "SUPPORT_V8" ON)

option(ENABLE_THREAD_SANITIZER "ENABLE_THREAD_SANITIZER" OFF)
option(ENABLE_ADDRESS_SANITIZER "ENABLE_ADDRESS_SANITIZER" OFF)
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ if (SUPPORT_NODE)
rtScriptNode/rtObjectWrapper.cpp rtScriptNode/rtWrapperUtils.cpp)
endif()

if (SUPPORT_V8)
message("Adding V8 scripting support")
add_definitions(-DRTSCRIPT_SUPPORT_V8)
set(PXCORE_FILES ${PXCORE_FILES} rtScriptV8/rtScriptV8.cpp rtScriptV8/jsCallback.cpp rtScriptV8/rtFunctionWrapper.cpp
rtScriptV8/rtObjectWrapper.cpp rtScriptV8/rtWrapperUtils.cpp)
endif()

if (PXCORE_MATRIX_HELPERS)
set(PXCORE_FILES ${PXCORE_FILES} pxMatrix4T.cpp)
Expand Down
6 changes: 6 additions & 0 deletions src/rtScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include "rtScriptNode/rtScriptNode.h"
#endif

#ifdef RTSCRIPT_SUPPORT_V8
#include "rtScriptV8/rtScriptV8.h"
#endif

#ifdef RTSCRIPT_SUPPORT_DUKTAPE
#include "rtScriptDuk/rtScriptDuk.h"
#endif
Expand Down Expand Up @@ -192,6 +196,8 @@ rtError rtScript::init()
createScriptDuk(mScript);
else
createScriptNode(mScript);
#elif defined(RTSCRIPT_SUPPORT_V8)
createScriptV8(mScript);
#elif defined(RTSCRIPT_SUPPORT_DUKTAPE)
createScriptDuk(mScript);
#elif defined(RTSCRIPT_SUPPORT_NODE)
Expand Down
156 changes: 156 additions & 0 deletions src/rtScriptV8/jsCallback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*

pxCore Copyright 2005-2018 John Robinson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include "jsCallback.h"
#include "rtWrapperUtils.h"


using namespace v8;

namespace rtScriptV8Utils
{

jsCallback::jsCallback(v8::Local<v8::Context>& ctx)
: mReq()
, mFunctionLookup(NULL)
, mIsolate(ctx->GetIsolate())
, mCompletionFunc(NULL)
, mCompletionContext(NULL)
{
mReq.data = this;

mContext.Reset(mIsolate, ctx);
}

jsCallback::~jsCallback()
{
mContext.Reset();
delete mFunctionLookup;
}

void jsCallback::enqueue()
{
uv_queue_work(uv_default_loop(), &mReq, &work, &doCallback);
}

void jsCallback::registerForCompletion(jsCallbackCompletionFunc callback, void* argp)
{
mCompletionFunc = callback;
mCompletionContext = argp;
}

void jsCallback::work(uv_work_t* /* req */)
{
}

jsCallback* jsCallback::create(v8::Local<v8::Context>& ctx)
{
return new jsCallback(ctx);
}

jsCallback* jsCallback::addArg(const rtValue& val)
{
mArgs.push_back(val);
return this;
}

Handle<Value>* jsCallback::makeArgs(Local<Context>& ctx)
{
Handle<Value>* args = new Handle<Value>[mArgs.size()];

for (size_t i = 0; i < mArgs.size(); ++i)
{
args[i] = rt2js(ctx, mArgs[i]);
}

return args;
}

jsCallback* jsCallback::setFunctionLookup(jsIFunctionLookup* functionLookup)
{
mFunctionLookup = functionLookup;
return this;
}

void jsCallback::doCallback(uv_work_t* req, int /* status */)
{
jsCallback* ctx = reinterpret_cast<jsCallback *>(req->data);

assert(ctx != NULL);
assert(ctx->mFunctionLookup != NULL);

rtValue ret = ctx->run();

if (ctx->mCompletionFunc)
{
ctx->mCompletionFunc(ctx->mCompletionContext, ret);
}

delete ctx;
}

rtValue jsCallback::run()
{
Locker locker(mIsolate);
Isolate::Scope isolate_scope(mIsolate);
HandleScope handle_scope(mIsolate);

Local<Context> ctx = PersistentToLocal(mIsolate, mContext);
Handle<Value>* args = this->makeArgs(ctx);
Local<Function> func = this->mFunctionLookup->lookup(ctx);

assert(!func.IsEmpty());
assert(!func->IsUndefined());

// This is really nice debugging
#if 0
Local<String> s = func->ToString();
String::Utf8Value v(s);
rtLogInfo("FUNC: %s", *v);
#endif

Local<Context> context = func->CreationContext();
Context::Scope contextScope(context);

Local<Value> val;

TryCatch tryCatch(mIsolate);
if (!func.IsEmpty())
{
// TODO: check that first arg. Is that 'this' why are we using context->Global()?
val = func->Call(context->Global(), static_cast<int>(this->mArgs.size()), args);
}

delete [] args;

rtValue returnValue;
if (tryCatch.HasCaught())
{
String::Utf8Value trace(tryCatch.StackTrace());
rtLogWarn("%s", *trace);
}
else
{
rtWrapperError error;
returnValue = js2rt(context, val, &error);
}

return returnValue;
}

} // namespace
79 changes: 79 additions & 0 deletions src/rtScriptV8/jsCallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*

pxCore Copyright 2005-2018 John Robinson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#ifndef RT_JAVASCRIPT_CALLBACK_H
#define RT_JAVASCRIPT_CALLBACK_H


#include "v8_headers.h"
#include <rtValue.h>
#include <vector>

namespace rtScriptV8Utils
{

typedef void (*jsCallbackCompletionFunc)(void* argp, rtValue const& result);

struct jsIFunctionLookup
{
virtual ~jsIFunctionLookup() { }
virtual v8::Local<v8::Function> lookup(v8::Local<v8::Context>& ctx) = 0;
};

struct jsCallback
{
virtual void enqueue();
void registerForCompletion(jsCallbackCompletionFunc callback, void* argp);
rtValue run();


static jsCallback* create(v8::Local<v8::Context>& ctx);

jsCallback* addArg(const rtValue& val);

jsCallback* setFunctionLookup(jsIFunctionLookup* functionLookup);

static void work(uv_work_t* req);
static void doCallback(uv_work_t* req, int status);

// made this public for the direct call (rtIsMain) path
virtual ~jsCallback();

protected:
virtual v8::Handle<v8::Value>* makeArgs(v8::Local<v8::Context>& ctx);

private:

std::vector<rtValue> mArgs;
uv_work_t mReq;
jsIFunctionLookup* mFunctionLookup;

// TODO: Is it ok to hold this pointer here?
v8::Persistent<v8::Context> mContext;
v8::Isolate* mIsolate;

jsCallbackCompletionFunc mCompletionFunc;
void* mCompletionContext;

jsCallback(v8::Local<v8::Context>& ctx);
};

} // namespace

#endif

Loading