【问题标题】:Keeping UINT64 values in V8在 V8 中保留 UINT64 值
【发布时间】:2015-03-19 11:00:31
【问题描述】:

我希望在我的 C/C++ 程序中集成一个脚本引擎。目前,我正在研究 Google V8。

如何在 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 位值类型(堆栈分配)来托管指针,但我假设其他引擎也希望跟踪其对象内的外部指针。

【问题讨论】:

    标签: v8 spidermonkey duktape


    【解决方案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));
    

    这当然远非高效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多