5

I just upgraded my V8 version to 3.20.16 (from some very old version). I can no longer use

Handle<Object> obj /* = ... */;
Persistent<Object> p = Persistent<Object>::New( obj );

to create a persistent handle to an object. The compiler suggests using static T* v8::Persistent<T>::New(v8::Isolate*, T*) [with T = v8::Object] instead. However, if I change my code to

Handle<Object> obj /* = ... */;
Persistent<Object> p = Persistent<Object>::New( Isolate::GetCurrent(), *obj );

the compiler complains that this function is private. How do I create a Persistent<Object> handle from a normal Handle<Object> now?

I've googled and the only thing I found was that the documentations seem to contradict each other:

thanks for any help in advance


As @aaronman pointed out, if you are working with integers only the fastest way to do this is to have all powers of 2 in table as there are not that many. By construction, in an unsigned 32 bit integer there are 32 powers of 2 (including the number 1), in a 64 bit integer there are 64 and so on.

But if you want to do it on the fly for a generic case you can easily calculate the surrounding powers of 2 of any number. In c/c++:

#include <math.h>

(...)

double bottom, top, number, exponent;

number = 1234;    // Set the value for number

exponent = int(log(number) / log(2.0));  // int(10.2691) = 10
bottom = pow(2, exponent);               // 2^10 = 1024
top = bottom * 2;                        // 2048

// Calculate the difference between number, top and bottom and add or subtract
// 1 accordingly
number = (top - number) <  (number - bottom) ? number + 1 : number - 1;
4

1 に答える 1

4

逆参照する必要のない通常を受け入れるコンストラクターがあります。Handle<T>

Persistent<Object>::New(Isolate::GetCurrent(), obj)

動作するはずです。

于 2013-08-20T15:14:40.983 に答える