Merged PR 266728: Move dispatcher to runtime folder

Move dispatcher to runtime folder
This commit is contained in:
Asi Bross 2017-05-15 21:40:38 +00:00 коммит произвёл Asi Bross
Родитель beac8fa9d3
Коммит 08e974ac2e
4 изменённых файлов: 22 добавлений и 21 удалений

Просмотреть файл

@ -1,18 +1,18 @@
function __napa_function_dispatcher__(func, args, contextHandle) {
var transport = require('napajs').transport;
var transport = require('napajs/lib/transport');
function __zone_function_main__(func, args, contextHandle) {
var transportContext = transport.createTransportContext(contextHandle);
var args = args.map((arg) => { return transport.unmarshall(arg, transportContext); });
return transport.marshall(func.apply(this, args), transportContext);
}
function __napa_module_dispatcher__(moduleName, functionName, args, contextHandle) {
function __zone_module_main__(moduleName, functionName, args, contextHandle) {
var module = require(moduleName);
var func = module[functionName];
if (!func) {
throw new Error("Cannot find function '" + functionName + "' in module '" + moduleName + "'");
}
return __napa_function_dispatcher__(func, args, contextHandle);
return __zone_function_main__(func, args, contextHandle);
}

Просмотреть файл

@ -27,14 +27,15 @@ void ExecuteTask::Execute() {
v8::HandleScope scope(isolate);
auto context = isolate->GetCurrentContext();
v8::Local<v8::Function> dispatcher;
v8::Local<v8::Function> zoneMainFunction;
std::vector<v8::Local<v8::Value>> args;
if (_module.empty()) {
auto dispatcherValue = context->Global()->Get(MakeExternalV8String(isolate, "__napa_function_dispatcher__"));
NAPA_ASSERT(dispatcherValue->IsFunction(), "dispatcher function must exist in global scope");
// Get the zone main function from global scope.
auto mainFunctionValue = context->Global()->Get(MakeExternalV8String(isolate, "__zone_function_main__"));
NAPA_ASSERT(mainFunctionValue->IsFunction(), "__zone_function_main__ function must exist in global scope");
dispatcher = v8::Local<v8::Function>::Cast(dispatcherValue);
zoneMainFunction = v8::Local<v8::Function>::Cast(mainFunctionValue);
auto funcValue = context->Global()->Get(MakeExternalV8String(isolate, _func));
if (!funcValue->IsFunction()) {
@ -46,11 +47,11 @@ void ExecuteTask::Execute() {
args.reserve(3); // (func, args, contextHandle)
args.emplace_back(v8::Local<v8::Function>::Cast(funcValue));
} else {
// Get the dispatcher function from global scope.
auto dispatcherValue = context->Global()->Get(MakeExternalV8String(isolate, "__napa_module_dispatcher__"));
NAPA_ASSERT(dispatcherValue->IsFunction(), "dispatcher function must exist in global scope");
// Get the module based main function from global scope.
auto mainFunctionValue = context->Global()->Get(MakeExternalV8String(isolate, "__zone_module_main__"));
NAPA_ASSERT(mainFunctionValue->IsFunction(), "__zone_module_main__ function must exist in global scope");
dispatcher = v8::Local<v8::Function>::Cast(dispatcherValue);
zoneMainFunction = v8::Local<v8::Function>::Cast(mainFunctionValue);
args.reserve(4); // (moduleName, functionName, args, contextHandle)
args.emplace_back(MakeExternalV8String(isolate, _module));
@ -69,7 +70,7 @@ void ExecuteTask::Execute() {
// Execute the function.
v8::TryCatch tryCatch(isolate);
auto res = dispatcher->Call(context->Global(), static_cast<int>(args.size()), args.data());
auto res = zoneMainFunction->Call(context->Global(), static_cast<int>(args.size()), args.data());
// Terminating an isolate may occur from a different thread, i.e. from timeout service.
// If the function call already finished successfully when the isolate is terminated it may lead

Просмотреть файл

@ -21,9 +21,9 @@ static void BroadcastFromFile(const std::string& file, Scheduler& scheduler, boo
std::mutex ZoneImpl::_mutex;
std::unordered_map<std::string, std::weak_ptr<ZoneImpl>> ZoneImpl::_zones;
// The path to the file containing the execute dispatcher function
static const std::string DISPATCHER_FILE = (boost::dll::this_line_location().parent_path().parent_path() /
"lib\\core\\napa-dispatcher.js").string();
// The path to the file containing the execute main function
static const std::string ZONE_MAIN_FILE = (boost::dll::this_line_location().parent_path().parent_path() /
"lib\\runtime\\zone-main.js").string();
std::shared_ptr<ZoneImpl> ZoneImpl::Create(const ZoneSettings& settings) {
std::lock_guard<std::mutex> lock(_mutex);
@ -77,9 +77,9 @@ void ZoneImpl::Init() {
// Create the zone's scheduler.
_scheduler = std::make_unique<Scheduler>(_settings);
// Read dispatcher file content and broadcast it on all workers.
// Read zone main file content and broadcast it on all workers.
// Do not set origin to avoid changing the global context path.
BroadcastFromFile(DISPATCHER_FILE, *_scheduler, false);
BroadcastFromFile(ZONE_MAIN_FILE, *_scheduler, false);
// Read bootstrap file content and broadcast it on all workers.
if (!_settings.bootstrapFile.empty()) {

Просмотреть файл

@ -33,8 +33,8 @@ TEST_CASE("tasks", "[tasks]") {
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope contextScope(context);
// Set a simple dispatcher function
BroadcastTask("function __napa_function_dispatcher__(func, args) { return func.apply(this, args); }").Execute();
// Set a simple zone main function
BroadcastTask("function __zone_function_main__(func, args) { return func.apply(this, args); }").Execute();
SECTION("load valid javascript") {
NapaResponseCode loadResponseCode;