2015-03-19 47 views
0

我期待在我的C/C++程序中集成腳本引擎。目前,我正在看Google V8。在V8中保留UINT64值

如何有效處理V8中的64位值?我的C/C++程序廣泛使用64位值來保持處理程序/指針。我不希望他們單獨分配在堆上。似乎有一個V8 :: External值類型。我可以將它分配給一個Javascript變量並將其用作值類型嗎?

function foo() { 

    var a = MyNativeFunctionReturningAnUnsigned64BitValue(); 

    var b = a; // Hopefully, b is a stack allocated value capable of 
       // keeping a 64 bit pointer or some other uint64 structure. 

    MyNativeFunctionThatAcceptsAnUnsigned64BitValue(b); 

} 

如果在V8中不可能,SpiderMonkey怎麼樣?我知道Duktape(Javascript引擎)有一個非Ecmascript標準的64位值類型(堆棧分配)給宿主指針,但我會假設其他引擎也想跟蹤其對象內部的外部指針。

回答

1

不,這是不可能的,恐怕duktape可能會違反規範,除非它花了很大的努力來確保它不可觀察。

您可以直接需要指針具有相同大小的物體上存儲對象的指針,以便存儲64位整數:

Local<FunctionTemplate> function_template = FunctionTemplate::New(isolate); 
// Instances of this function have room for 1 internal field 
function_template->InstanceTemplate()->SetInternalFieldCount(1); 

Local<Object> object = function_template->GetFunction()->NewInstance(); 
static_assert(sizeof(void*) == sizeof(uint64_t)); 
uint64_t integer = 1; 
object->SetAlignedPointerInInternalField(0, reinterpret_cast<void*>(integer)); 
uint64_t result = reinterpret_cast<uint64_t>(object->GetAlignedPointerInInternalField(0)); 

這當然被有效爲止。