網(wǎng)絡(luò)部分暫且放一放
還是setup()的下一步
V8 is Google's open source JavaScript engine.
n.nvm = nvm.NewNebulasVM()
type NebulasVMstruct{}
// NewNebulasVM create new NebulasVM
func NewNebulasVM() core.NVM {
return &NebulasVM{}
}
初始化一個VM
if err = n.nvm.CheckV8Run(); err != nil {
logging.CLog().WithFields(logrus.Fields{
"err": err,
}).Fatal("Failed to setup V8.")
}
看一下V8環(huán)境變量是否正確設(shè)置
接下來看一下
文件結(jié)構(gòu)
nf/nvm/
lib ____js庫
lnvm_____沒有用
native_____v8動態(tài)鏈接庫以及星云鏈橋接的c++代碼
test————測試代碼
v8_______js庫和go實現(xiàn)的橋接邏輯
blockchain___blockchain相關(guān)函數(shù)
context____NVM的context
crypto______加密相關(guān)函數(shù)
engine_____NVM接口實現(xiàn)
enginev8____smartcontractengine 接口實現(xiàn)
event___事件相關(guān)函數(shù)
module__模塊相關(guān)函數(shù)
storage____賬戶儲存相關(guān)函數(shù)
types_____類型相關(guān)定義
接下有關(guān)V8的幾個API
isolate context
isolate有完整的V8實例,有完整的堆和棧酿雪。context是一個上下文,js代碼都是在一個context中運行的
https://stackoverflow.com/questions/19383724/what-exactly-is-the-difference-between-v8isolate-and-v8context
上邊有他們的區(qū)別
一個isolate某一時刻只有一個runtime?
包括但不限于一個heap管理器,垃圾回收等
一段時間內(nèi)涩拙,有且只有一個線程能使用此isolate摄杂,不過多個線程可以同時使用多個isolate
單獨的isolate不足以運行腳本,需要一個全局對象。context就是這樣的一個提供全局對象的工具
對于一個給定的isolate不僅可以有多個context,并且這些context可以共享某個對象
handle和handle scope
handle是一個js對象的索引
它指向js對象在v8管理的heap中的位置
handle不存在heap中膝迎,而是存在于stack中。只有一個Handle被釋放后才能從stack中推出胰耗。
有時候需要聲明很多handle限次。未免太麻煩,所以就有了handle scope
template模板
模板(Template)用在定義一個Context下的JS的對象或者函數(shù)柴灯。Funct?ionTemplate將
C++的函數(shù)暴露給JS卖漫。每個Funct?ionTemplate對應(yīng)-?-個0bj?ectTemplate费尽。
Funct?ionTemplate相當(dāng)于0bj?ectTemp?late的構(gòu)造函數(shù)。Template?的示例如下:
創(chuàng)建一個模板實例
v8: :Handle<v8:: objectTemplate> global = v8: :ObjectTemplate: :New();
將我們之前實現(xiàn)的Plus函數(shù)模板懊亡,與JavaScript的plus函數(shù)關(guān)聯(lián)起來依啰,相當(dāng)于其回調(diào)函數(shù)
global->Set(v8: :String: :New("plus"), v8:: FunctionTemplate: :New(Plus));
accessor
一個accessor是c++的一個回調(diào)函數(shù),當(dāng)javascript訪問一個對象屬性時。它被回調(diào)并返回一個值。accessor通過object template來設(shè)置
accessor需要通過一個object template來設(shè)置 使用setAccessor 該方法需要提供一個屬性和關(guān)聯(lián)的兩個回調(diào)作為參數(shù)
type? V8Engine? struct {
ctx? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *Context? ? //context
modules? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Modules? ? ?//模塊
v8engine? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *C.V8Engine? ? //? v8中的V8engine
strictDisallowUsageOfInstructionCounter int? ? //
enableLimits? ? ? ? ? ? ? ? ? ? ? ? ? ? bool
limitsOfExecutionInstructions? ? ? ? ? uint64
limitsOfTotalMemorySize? ? ? ? ? ? ? ? uint64
actualCountOfExecutionInstructions? ? ? uint64
actualTotalMemorySize? ? ? ? ? ? ? ? ? uint64
lcsHandler? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uint64
gcsHandler? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uint64
innerErrMsg? ? ? ? ? ? ? ? ? ? ? ? ? ? string
innerErr? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error
}