Deleting a DontDelete property is not possible without breaking the object
model (which in turn means random crashes). If an object has a DontDelete
property, the compiler might decide to inline the access to that property
(because why check, it can never be deleted). If you then ForceDelete you,
that code is incorrect.

We tried various workarounds, including discarding all compiled code when
ForceDelete was used (same holds true for ForceSet btw), but in the end,
the right solution is to design your application and its API such that it
conforms the the JS object model.

best
-jochen

On Sun, Mar 13, 2016 at 9:28 AM Danny Dorfman <[email protected]>
wrote:

> Hello there,
>
> I have a situation, where an object's property is initially declared as
> DontDelete, but later needs to be *removed*, so that I can create a named
> accessor with the same name.
> The V8 API used to have a ForceDelete() method that did the trick, but now
> I can not find it any more (I recently upgraded to 4.9).
> What is the modern equivalent of ForceDelete()?
>
> Thanks in advance...
>
>
> Sample code:  // I tried using the regular Delete() method, but it fails.
> The output is "Error in Delete! (type 2)"
>
> #include <iostream>
> #include <string.h>
> #include "libplatform/libplatform.h"
> #include "v8/v8.h"
>
> void GetNamedAccessor(v8::Local<v8::Name> name, const
> v8::PropertyCallbackInfo<v8::Value>& info)
> {
>   std::cout << "[" << std::hex << info.This()->GetIdentityHash() << "]
> GetNamedAccessor, name=" << *v8::String::Utf8Value(name) << std::endl;
> }
>
> void SetNamedAccessor(v8::Local<v8::Name> name, v8::Local<v8::Value>
> value, const v8::PropertyCallbackInfo<void>& info)
> {
>   std::cout << "[" << std::hex << info.This()->GetIdentityHash() << "]
> SetNamedAccessor, name=" << *v8::String::Utf8Value(name)
>             << ", value=" << *v8::String::Utf8Value(value) << std::endl;
> }
>
> class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
>  public:
>   virtual void* Allocate(size_t length) {
>     std::cout << "!!! Allocate runs !!! length = " << length << std::endl;
>     void* data = AllocateUninitialized(length);
>     return data == NULL ? data : memset(data, 0, length);
>   }
>   virtual void* AllocateUninitialized(size_t length) { return
> malloc(length); }
>   virtual void Free(void* data, size_t) { free(data); }
> };
>
> int main(int argc, char* argv[])
> {
>   // initialize v8
>   v8::V8::InitializeICU();
>   v8::V8::InitializeExternalStartupData(argv[0]);
>   v8::Platform* platform = v8::platform::CreateDefaultPlatform();
>   v8::V8::InitializePlatform(platform);
>   v8::V8::Initialize();
>   ArrayBufferAllocator allocator;
>   v8::Isolate::CreateParams create_params;
>   create_params.array_buffer_allocator = &allocator;
>   v8::Isolate* isolate = v8::Isolate::New(create_params);
>   v8::Isolate::Scope isolate_scope(isolate);
>   v8::HandleScope handle_scope(isolate);
>   v8::Local<v8::Context> context = v8::Context::New(isolate);
>   v8::Context::Scope context_scope(context);
>
>   // prepare object with non-deleteable property
>   v8::Local<v8::String> fooStr = v8::String::NewFromUtf8(isolate,"foo");
>   v8::Local<v8::String> barStr = v8::String::NewFromUtf8(isolate,"bar");
>   v8::Local<v8::Object> obj = v8::Object::New(isolate);
>   v8::Maybe<bool> ok = obj->DefineOwnProperty(context, fooStr, barStr,
> v8::DontDelete);
>   if (ok.IsNothing())
>   {
>     std::cout << "Error in DefineOwnProperty!" << std::endl;
>     return 1;
>   }
>
>   // do some testing
>   ok = obj->Delete(context, fooStr);
>   if (ok.IsNothing())
>   {
>     std::cout << "Error in Delete! (type 1)" << std::endl;
>     return 1;
>   }
>   if (ok.FromJust() == false)
>   {
>     std::cout << "Error in Delete! (type 2)" << std::endl;
>     return 1;
>   }
>   ok = obj->SetAccessor(context, fooStr, GetNamedAccessor,
> SetNamedAccessor, v8::MaybeLocal<v8::Value>(),
>
> v8::AccessControl(v8::ALL_CAN_READ|v8::ALL_CAN_WRITE),
> v8::PropertyAttribute(6));
>   if (ok.IsNothing())
>   {
>     std::cout << "Error in SetAccessor! (type 1)" << std::endl;
>     return 1;
>   }
>   if (ok.FromJust() == false)
>   {
>     std::cout << "Error in SetAccessor! (type 2)" << std::endl;
>     return 1;
>   }
>   return 0;
> }
>
> --
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to