Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:33:05+00:00 2026-06-07T19:33:05+00:00

I have several simple C++ classes, for example: class Audio { public: Audio(const char

  • 0

I have several simple C++ classes, for example:

class Audio {
public:
    Audio(const char *filename, bool async = true);
    ~Audio();

    Audio *play(int fade = 0);
    Audio *pause();
    Audio *loop(int loops = -1);
    Audio *volume(float volume);

I have replicated the structure in JavaScript as follows:

var Audio = function(filename, async) {};
Audio.prototype.Play = function(fade) {};
Audio.prototype.Pause = function() {};
Audio.prototype.Loop = function(loops) {};
Audio.prototype.Volume = function(volume) {};

And after reading both the documentation and the sources for v8, v8-juice, and a plethora of blogs… I can’t find a single reference on how to “override” a JS function with a C++ method.

Ideally, I’d like JS to be in control of class creation/destruction (is this possible?), and have those objects always point to my native functions (PrototypeTemplate?).

I’ve seriously spent my entire day today reading articles/blogs/code related to this and can’t find, what I should hope is, a simple answer.


For your sakes, a “simple” answer to me would be something along these lines (wrappers are fine with me; if I have to write wrappers for the creation/destruction that’s okay):

v8::Local<v8::Function> jsAudioFunction = v8::Local<v8::Function>::Cast(v8::Context::GetCurrent()->Global()->Get(v8::String::New("Audio")));
jsAudioFunction->Setup(/* setup constructor/destructor */);
jsAudioFunction->SetPrototype(/* map native methods to js functions */);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:33:07+00:00Added an answer on June 7, 2026 at 7:33 pm

    While this doesn’t answer the question of binding native code to JS objects, here are the fruits of my labor:

    static void jsAudioGC(v8::Persistent<v8::Value> object, void *data) {
        v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(object);
        Audio *audio = static_cast<Audio*>(obj->GetPointerFromInternalField(0));
        if (audio != NULL) {
            obj->SetPointerInInternalField(0, NULL);
            v8::V8::AdjustAmountOfExternalAllocatedMemory(-sizeof(audio));
            delete audio;
        }
        object.Dispose();
    }
    
    v8::Handle<v8::Value> jsAudio(const v8::Arguments &args) {
        v8::Persistent<v8::Object>::New(args.This()).MakeWeak(NULL, jsAudioGC);
    
        Audio *audio = new Audio(get(args[0], ""), get(args[1], true));
        v8::V8::AdjustAmountOfExternalAllocatedMemory(sizeof(audio));
        args.This()->SetPointerInInternalField(0, audio);
        return args.This();
    }
    
    v8::Handle<v8::Value> jsAudioPlay(const v8::Arguments &args) {
        Audio *audio = static_cast<Audio*>(args.This()->GetPointerFromInternalField(0));
        if (audio != NULL) audio->play(get(args[0], 0));
        return args.This();
    }
    

    Inside of my init() function:

    v8::Handle<v8::FunctionTemplate> audio = v8::FunctionTemplate::New(&jsAudio);
    audio->PrototypeTemplate()->Set("Play", v8::FunctionTemplate::New(&jsAudioPlay));
    audio->InstanceTemplate()->SetInternalFieldCount(1);
    globals->Set("Audio", audio);
    

    This does everything exactly the way I want it to; including proper instantiation and garbage collection.

    The only regret I have with this approach is that I would like to be able to “only use what is defined.” That way these function are only available if the JS “class” has been included (making it possible to have all functions defined and documented in a JS IDE).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: [DataContract] [KnownType(typeof(SpecifiedItemCollection))] public class ItemCollection<T> : IEnumerable where T
A simple question here, I have several classes in my code, but only one
This seems like such a simple question. I have several Edit boxes on my
My question is deceptively simple, but I have lost several hours of study trying
I have a simple application written in C# and .Net 2.0 that displays several
A simple question. I have an ASP.NET web application which contains several assemblies and
Here's a very simple example of what I'm trying to get around: class Test(object):
I have several small classes that are all peers of each other declared and
I have several functions in an assembly dll named UserFunctions.dll , for example :
I have a base class and several subclasses derived from that base class. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.