Is there a way to make a class method accessible in V8?

How can I make a class method accessible in a V8 context?

I am trying to achieve something like the line below, but it's not working because lambda with captures cannot be treated as a function pointer.

Global()->Set("foo",v8::FunctionTemplate::New([this](const v8::Arguments &args) -> v8::Handle<v8::Value>{this->foo()}; return v8::Undefined()));

I'm not talking about exposing literal types like struct Point {int x,y}, but specifically about exposing a class method or a simple callable object.

Answer №1

This is how I managed to make it happen.

context->Global()->Set(v8::String::New("_this"),v8::External::Wrap(this));
context->Global()->Set(v8::String::New("foo"),v8::FunctionTemplate::New(
    [](const v8::Arguments  &args) -> v8::Handle<v8::Value>{
         reinterpret_cast<Foo*>(reinterpret_cast<v8::External*>(*(args.This()->Get(v8::String::New("engine"))))->Value())->foo();
         return v8::Undefined();
})->GetFunction());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to retrieve a variable within the init() function

My current project involves using datatables along with ajax to display information dynamically. Below is the code snippet I am working with: // Setting up the module var DatatableAdvanced = function() { // Examples of Basic Datatables var _c ...

Convert a function into another function when the button is clicked

Hey there, I have an array containing years in my controller: public function getNextYear() { $years = $this->getYears(); foreach ($years as $key => $value) { $y[] = $value + 1; } return $y; } I am displaying this on my ind ...

Locate a specific item in an array using AngularJs based on a key and present its value on the View

Imagine you have an array of objects: $scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}]; And a variable that corresponds to key. For instance: $scope.a = 3; In the Controller, you want ...

Is there a method to initiate a 'simple' action when dispatching an action in ngrx?

Here's the scenario I'm facing: When any of the actions listed below are dispatched, I need to update the saving property in the reducer to true. However, currently, I am not handling these actions in the reducer; instead, I handle them in my ef ...

What happens with JQuery if the JQuery IIFE does not get executed properly?

The jquery-3.3.1.js file contains the following code: ( function( global, factory ) { ... } ); Within this Immediately-Invoked Function Expression (IIFE), there is a line allowing for the use of the JQuery and $ variables to call the JQuery function: wi ...

difficulty encountered when passing session variable on PHP pages

In my project, I have two important PHP pages: quizaction.php and result.php. The functionality involves passing variables from quizaction.php to result.php. Below is an overview of my code: <?php include 'db.php'; session_start( ...

"The occurrence of a `POST` error due to failed XHR loading can be attributed to Ajax

Attempting to execute a straightforward Ajax Request to transmit a JSON object from my Javascript file to my Python file in Django has presented a challenge. Occasionally, an error message XHR failed loading: POST pops up while executing it in the manner b ...

An error occurs when attempting to redirect with getServerSideProps

When I am logged in, I want to redirect to the /chat page using auth0 for authentication. The error seems to be related to returning an empty string for props, but it does not impact the website as redirection works correctly. The main issue here is the in ...

The application experiences crashes when the tablet is rotated, happening while in the development stage

For my web-based Android application project, I am utilizing PhoneGap and Eclipse IDE on a Windows platform. My focus is specifically on Tablet development, more precisely on the Samsung Galaxy Tab 10.1. To develop, I use Eclipse and test the app on the ...

AngularJS: The blend of bo-bind, bindonce, and the translate filter

I am currently working with angular 1.2.25, angular-translate 2.0.1, angular-translate-loader-static-files 2.0.0, and angular-bindonce 0.3.1. My goal is to translate a static key using bindonce. Here is the code snippet I have: <div bindonce> < ...

Problem with dynamic page routes in Next.js (and using TypeScript)

Hi everyone, I'm currently learning next.js and I'm facing an issue while trying to set up a route like **pages/perfil/[name]** The problem I'm encountering is that the data fetched from an API call for this page is based on an id, but I wa ...

Exposed object accessible from the jQuery plugin

My experience with two jQuery plugins has been quite interesting. The first one: ;(function($){ function CbUploader(element,options){ widget=this; ..... } $.fn.cbUploader = function(options){ new CbUploader(this,option ...

When a legend is clicked, it should display only the selected item while hiding all other legends automatically in a chart created

I have a highchart with 10 legends. When I click on the first legend, only that legend should remain visible while the rest are automatically hidden... Below is a code snippet with two legends: $(function() { var chart = $('#container').hig ...

Both GCC and Clang appear to disregard overload resolution when it comes to the allocation function call

Check out this specific case #include <iostream> struct A{ void* operator new(std::size_t N, std::align_val_t){ // #1 return malloc(sizeof(char)* N); } }; int main(){ auto ptr = new A; // #2 } Both GCC and Clang are giving er ...

Is it possible to align an image that uses position:relative with one that uses position:absolute?

On a webpage, I have placed two images. The first image is set to position:relative and the second image is positioned right below it with position:absolute. How can I ensure that as you zoom in or out, the second image remains aligned with the first ima ...

Encountering an issue when attempting to bring in a new library

When attempting to import a library, I encountered this error. https://i.sstatic.net/NYYQX.png The command used to obtain this library was: npm i queue Here is how I attempted to import it in my javascript: import Queue from "./node_modules/queue/ ...

Issue encountered while using AJAX to load images

Currently, I am utilizing a jQuery plugin to fetch images from Flickr. My aim is to organize these images into 3 columns dynamically as they are loaded, ensuring that each image is placed in the shortest column to achieve close to equal lengths for all col ...

Tips for resolving import errors encountered after running npm start

I recently started using React and I am currently following a tutorial. Even though I have the exact same code as the tutorial, I'm encountering the following error. ./src/index.js Attempted import error: './components/App' does not have a ...

Sending information to ajax request upon successful completion

I needed to transfer the output of the json_encode() function to jQuery. Once successful, I intended to refresh a specific division. Action: <a id="js-delete-file" href="#" data-url="<?php echo site_url('document_items/remove/'. $value[&a ...

I am having difficulty toggling text within a for loop in JavaScript

When I display a list of cards using a for loop and click on them, I want to toggle some text. The issue I'm facing is that it works fine for the top card, but when I click on the card below, it toggles the text of the first card instead. This is desp ...