by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    xiolec
    @xiolec
    I tried that but it still doesn't work
    I.e. with that Node.ELEMENT_TYPE still wont come up. it would come up as undefined.
    xiolec
    @xiolec
    Only thing I was thinking is that it could be because the object that is set on the context is just the function for the js_function_template.
    xiolec
    @xiolec
    Okay so I figured out that this actually works -- Node.prototype.ELEMENT_TYPE
    but I need to be able to access it directly without it being on the prototype
    xiolec
    @xiolec
    Okay lol I figured out how to get it to work. It had to be set directly on the js_function_template. Instead of the class_function_template
    template<typename Value>
        class_& set_js_const(char const* name, Value value)
        {
            v8::HandleScope scope(isolate());
            class_singleton_.js_function_template()->Set(v8pp::to_v8(isolate(), name),
                to_v8(isolate(), value), v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
            return *this;
        }
    and also not being set on the prototype
    xiolec
    @xiolec
    Update: it only works for the base class that its set on. So it doesn't work for inherited classes. I.e. classes that are inherited from Node wont have the const values
    though looking into what I want to do a little bit more that should be exactly how it should be
    Pavel Medvedev
    @pmed
    It's strange. I thought the inheritance in JS via prototype works this way
    Bu I never used this with derived classes
    xiolec
    @xiolec
    I guess since that gets set directly on the function instead of the prototype that it doesn't actually get inherited so It gives the objects to function itself
    xiolec
    @xiolec
    You wouldn't happen to know how to call a function within the scope of an object would you? say -- callingobject.func() = function(){this;} -- 'this' would result in the callingobject and not the global object. like I could call it from javascript with callingobject.func(); which would work fine, but if I have the function saved in c++ code and call the function it would result in the 'this' = global object.
    wait is that what the rev value on v8::function.call is for?
    *recv
    Pavel Medvedev
    @pmed
    yes, recv it is. there is also handy v8pp::call_v8() that allows to call a V8::Function with C++ arguments, converting them to V8::Values:
    v8::Local<v8::Function> fun;
    v8::Local<v8::Object> fun_this;
    
    v8::Local<v8::Value> ret = v8pp::call_v8(isolate, fun, fun_this, 11, "str", you_wrapped_cpp_object);
    This message was deleted
    xiolec
    @xiolec
    do you know how to override the in function on c++ objects
    var enumeratedArray = new Array();
            for (var n in object)
                enumeratedArray.push(n);
    So I want to be able to override "in" so that i can send the values that I want back
    Pavel Medvedev
    @pmed
    I suppose you need to have a custom keys function exposed from your wrapped C++ class. But I never tried this though.
    xiolec
    @xiolec
    nope no luck with that. I'm trying to implement the interceptors right now. Hopefully the enum interceptor is what I want
    xiolec
    @xiolec
    okay I got interceptors to work and it looks like that solves the problem
    xiolec
    @xiolec
    so interceptors seem to only work fully when on the js_function_template, but that doesn't hold the class object. What is the purpose of having both of the function templates? Would it be possible if I wanted to combine them into one? are there any problems that I would encounter?
    xiolec
    @xiolec
    okay I added a objecttemplate to the class_ and applied the interceptors to that. Everything is almost working. Though I also disabled the js_function... just to see what it would do... so far nothing. Was there a purpose for the js_function?
    Pavel Medvedev
    @pmed
    I used it as a well known way to wrap C++objects: http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/
    xiolec
    @xiolec
    oh okay so it's just used for a constructor
    xiolec
    @xiolec
    so if I'm understanding it correctly. The function constructor gets called everytime a new instance of that function is created. So you need to let the function know if it's wrapping or creating a new object then wrapping.
    Do you know what happens with inherited constructors? are they just ignored?
    xiolec
    @xiolec

    so i changed the class function to this

    v8::Local<v8::FunctionTemplate> func = v8::FunctionTemplate::New(isolate_,//);
            //v8::Local<v8::FunctionTemplate> js_func = v8::FunctionTemplate::New(isolate_,
                [](v8::FunctionCallbackInfo<v8::Value> const& args)
                {
                    v8::Isolate* isolate = args.GetIsolate();
                    try
                    {
                        return args.GetReturnValue().Set(instance(isolate).wrap_object(args));
                    }
                    catch (std::exception const& ex)
                    {
                        args.GetReturnValue().Set(throw_ex(isolate, ex.what()));
                    }
                });

    setting the constructor on the class_function_template. then I'm also creating an object template after that

    func->InstanceTemplate()->SetInternalFieldCount(2);
            v8::Local<v8::ObjectTemplate> obj = v8::ObjectTemplate::New(isolate_, class_function_template());
            obj_temp_.Reset(isolate_, obj);

    and in the wrap function I changed it to

    v8::EscapableHandleScope scope(isolate_);
            v8::Local<v8::Object> obj = object_template()->NewInstance();
                //class_function_template()->GetFunction()->NewInstance();
            obj->SetAlignedPointerInInternalField(0, object);
            obj->SetAlignedPointerInInternalField(1, this);

    creating the new instance of the object template appears to not call the constr. I tested it out and so far I don't see any problems with this. Is there anything that you can think of that will go wrong?

    xiolec
    @xiolec
    and i just commented out the js_function to disable it
    xiolec
    @xiolec
    Found a couple memory leaks. The singleton class wasn't being deleted when the isolate is destroyed. Which i managed to fix. and the property data container has like 1700 instances after one run lol.
    Pavel Medvedev
    @pmed
    I think to review class wrapping in some time, the code there seems too complicated.
    Pavel Medvedev
    @pmed
    Yes, class singletons cause memory leaks since they use dynamic memory allocation. But I don't know when and where destroy them because v8::Isolate is created and destroyed outside of the library. I can just add a function to manually cleanup singletons in v8pp for specified isolate.
    What property data container do you mean?
    xiolec
    @xiolec
    struct property_ the call to set_external_data is not calling the delete function when its done.
    xiolec
    @xiolec
    ah yeah I just added the cleanup for the singletons into the context destructor and checked if it had its own isolate. Though If you created the isolate else where it still wouldn't clean up lol. I should get around to uploading all my code lol
    xiolec
    @xiolec
    so I'm not sure what's happening with the property. The set weak on the external data is not being called. Though I did read in the v8 docs that set weak shouldn't be relied on. So, I think I'm going to scrap that and just add all the external data to the class singletons which would then get deleted when the singletons are deleted. or at least that's what I'm going to try
    xiolec
    @xiolec
    wait the properties are actually getting deleted when v8 does full garbagecollection
    except its crashing lol
    Pavel Medvedev
    @pmed
    Well, all I can do on the library side is to add a function to manually unregister v8pp::class in a specified v8::Isolate. There is a similar function v8pp::class::delete_objects which deletes all wrapped C++ objects. Of course access from JavaScript to such wrapped C++ objects would crash V8
    Pavel Medvedev
    @pmed
    properties require to store 2 pointers to member functions. This at least twice more than the v8::External can store. So I use dynamic memory allocation in set_external_data and make such an External a weak reference. You are right and weak references are garbage collected sometimes on full GC.
    Pavel Medvedev
    @pmed
    actually, when a v8::Isolate has a long live time and there are no many v8pp bindings, I don't think this is a significant issue with the non-freed dynamic memory
    xiolec
    @xiolec

    So, anyways I put the dynamic external data into a static class per isolate and just deleted them when the isolate was done. Which works fine for me now. Though I'm curious as to why it was crashing to begin with.

    So, when the weak callback was called to delete the external data it went into that function and deleted the external data fine. then when exiting it went back into global-handlers.cc -PostGarbageCollectionProcessing function. and crashes at CHECK(state() != NEAR_DEATH); Do you know what would cause it to crash here?

    Pavel Medvedev
    @pmed
    I don't know the cause, probably it's an internal GC check in V8 debug version
    btw, I added a v8pp::class_::destroy() function to delete all wrapped C++objects and the class_ instance
    xiolec
    @xiolec

    I uploaded my changes to github. my version of v8pp::context is not finished though. A lot of that was testing around to see what works. Though I eventually will add a callback function for context creation that would allow you to create the global object as a v8pp::class_.

    I added interceptors to v8pp::class_. which is similar to the property. any_object.h was just stuff I was testing out. hopefully allowing a cpp function to accept either a cpp object or a v8 object. I just added v8pp::Function to that file which allows for accepting a function that is either from v8 or from cpp and if the object is passed to v8 with a cpp function that function would be converted so that It could be used within v8. also with that I enabled using std::function as a function withing v8pp.

    reference_tracker.h is just for debugging memory leaks. I should probably change it to a macro.

    v8object_base allows for a persistent object to be stored for a class. I.e. if you put it as a base class on an object that object will remain in memory until it the object is destroyed in cpp. destroying the isolate that the object is attached to also destroys the persistent object. class can also detect if the a class has the base class then just get the v8 object from there instead of searching for the v8 object.
    v8ppdebug.h was just some interceptors that I was using for debugging. they get added to every class object that doesn't already have interceptors when the debugging is enabled.
    xiolec
    @xiolec

    I also added some destroy functions in class_ though those were kinda just put together quickly. also added set_js_const which allows to add a const value on to the js function.

    the interceptors don't include all the static asserts needed. Haven't really tried merging it to your version yet. I need to redo all my changes to v8pp::context first lol.