github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/event.cc (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see 17 // <http://www.gnu.org/licenses/>. 18 // 19 20 #include "event.h" 21 #include "../engine.h" 22 #include "global.h" 23 #include "instruction_counter.h" 24 25 static EventTriggerFunc TRIGGER = NULL; 26 27 void InitializeEvent(EventTriggerFunc trigger) { TRIGGER = trigger; } 28 29 void NewNativeEventFunction(Isolate *isolate, Local<ObjectTemplate> globalTpl) { 30 globalTpl->Set(String::NewFromUtf8(isolate, "_native_event_trigger"), 31 FunctionTemplate::New(isolate, EventTriggerCallback), 32 static_cast<PropertyAttribute>(PropertyAttribute::DontDelete | 33 PropertyAttribute::ReadOnly)); 34 } 35 36 void EventTriggerCallback(const FunctionCallbackInfo<Value> &info) { 37 Isolate *isolate = info.GetIsolate(); 38 Local<Context> context = isolate->GetCurrentContext(); 39 40 if (info.Length() < 2) { 41 isolate->ThrowException(Exception::Error( 42 String::NewFromUtf8(isolate, "_native_event_trigger: mssing params"))); 43 return; 44 } 45 46 Local<Value> topic = info[0]; 47 if (!topic->IsString()) { 48 isolate->ThrowException(Exception::Error(String::NewFromUtf8( 49 isolate, "_native_event_trigger: topic must be string"))); 50 return; 51 } 52 53 Local<Value> data = info[1]; 54 if (!data->IsString()) { 55 isolate->ThrowException(Exception::Error(String::NewFromUtf8( 56 isolate, "_native_event_trigger: data must be string"))); 57 return; 58 } 59 60 if (TRIGGER == NULL) { 61 return; 62 } 63 64 V8Engine *e = GetV8EngineInstance(context); 65 String::Utf8Value sTopic(topic); 66 String::Utf8Value sData(data); 67 68 size_t cnt = 0; 69 TRIGGER(e, *sTopic, *sData, &cnt); 70 71 // record event usage. 72 IncrCounter(isolate, isolate->GetCurrentContext(), cnt); 73 }