github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/global.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 "global.h" 21 #include "blockchain.h" 22 #include "event.h" 23 #include "instruction_counter.h" 24 #include "log_callback.h" 25 #include "require_callback.h" 26 #include "storage_object.h" 27 #include "crypto.h" 28 #include "random.h" 29 30 Local<ObjectTemplate> CreateGlobalObjectTemplate(Isolate *isolate) { 31 Local<ObjectTemplate> globalTpl = ObjectTemplate::New(isolate); 32 globalTpl->SetInternalFieldCount(1); 33 34 NewNativeRequireFunction(isolate, globalTpl); 35 NewNativeLogFunction(isolate, globalTpl); 36 NewNativeEventFunction(isolate, globalTpl); 37 // NewNativeRandomFunction(isolate, globalTpl); 38 39 NewStorageType(isolate, globalTpl); 40 41 return globalTpl; 42 } 43 44 void SetGlobalObjectProperties(Isolate *isolate, Local<Context> context, 45 V8Engine *e, void *lcsHandler, 46 void *gcsHandler) { 47 // set e to global. 48 Local<Object> global = context->Global(); 49 global->SetInternalField(0, External::New(isolate, e)); 50 51 NewStorageTypeInstance(isolate, context, lcsHandler, gcsHandler); 52 NewInstructionCounterInstance(isolate, context, 53 &(e->stats.count_of_executed_instructions), e); 54 uint64_t build_flag = e->ver; 55 if (BUILD_MATH == (build_flag & BUILD_MATH)) { 56 NewRandomInstance(isolate, context, lcsHandler); 57 } 58 if (BUILD_BLOCKCHAIN == (build_flag & BUILD_BLOCKCHAIN)) { 59 NewBlockchainInstance(isolate, context, lcsHandler, build_flag); 60 } 61 62 NewCryptoInstance(isolate, context); 63 } 64 65 V8Engine *GetV8EngineInstance(Local<Context> context) { 66 Local<Object> global = context->Global(); 67 Local<Value> val = global->GetInternalField(0); 68 69 if (!val->IsExternal()) { 70 return NULL; 71 } 72 73 return static_cast<V8Engine *>(Local<External>::Cast(val)->Value()); 74 }