Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Memory layout of JavaScript objects in V8

I'm looking to writing some C bindings to V8, and so I'll need to figure out the memory layout of the various primitive JavaScript types. Is there any documentation on these details anywhere?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You don't need to know data type design to write C bindings for V8. Objects are never actually accessed directly when working with V8, but through an API - only the V8 implementation knows how they are distributed. For example, getting a foo o of an object looks like this in C++:

 v8::Handle<v8::Object> o; v8::Handle<v8::Object> v = o->Get(v8::String::NewFromUtf8(isolate, "foo"));

Now to wrap this in C you just need to know how to render and pass v8::Handle<T> , then you can write wrappers like:

 template<typename T> Handle<T> FromC(v8_handle_t h) { /* ... */ } template<typename T> v8_handle_t ToC(Handle<T> h) { /* ... */ } extern "C" v8_handle_t v8_object_get(v8_handle_t self, v8_handle_t key) { return ToC(FromC<Object>(self)->Get(FromC<Value>(key))); }

So what is inside v8::Handle<T> ? It's really just a pointer to a slot which, in turn, contains an actual pointer to an underlying V8 object. This double indirection exists to allow the V8 GC to accurately track which objects are in use in the C++ code and to allow you to move these objects around.

So in theory you can define v8_handle_t as an opaque pointer and order it like this:

 typedef struct v8_object_pointer_t* v8_handle_t; // Opaque pointer static_assert(sizeof(v8_handle_t) == sizeof(Handle<Object>)) template<typename T> Handle<T> FromC(v8_handle_t h) { return *(Handle<T>*)&h; } template<typename T> v8_handle_t ToC(const Handle<T>& h) { return *(v8_handle_t*)&h; }

A minor complication comes from managing the structure called HandleScope that manages the Handle s. In the C++ API, it relies on the RAII pattern to manage some backing storage. The easiest way to deal with this is probably:

 typedef struct { void* a[3]; } v8_handle_scope_t; static_assert(sizeof(v8_handle_scope_t) == sizeof(HandleScope)) void v8_handle_scope_enter(v8_handle_scope_t* scope) { new(scope) HandleScope; } void v8_handle_scope_leave(v8_handle_scope_t* scope) { delete (HandleScope*)scope; }

With carefully balanced usage in code that needs to handle scopes:

 for (i = 0; i < N; i++) { v8_handle_scope_t scope; v8_handle_scope_enter(&scope); // action v8_handle_scope_leave(&scope); }
over 4 years ago · Santiago Trujillo Report

0

As your question is about how to write V8 C++ addons for Node.js you should just refer to the manual for nodeJs which has a reasonably simple guide for writing addons;

If you question is about how to make background threads then there are nodejs plugins which helps you with that, but also read this for how to write it directly.

You should never try to access the memory for V8 directly, as the memory and object may move --always use the APIs

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!