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

201
Views
Emscripten Embind namespace methods within object

I've been experimenting with Emscripten to potentially bring a C++ API to web assembly.

I've successfully been able to export individual functions and classes from my API to the JS module, but I'm not so sure how to Embind full namespaces of methods as their own JS objects.

Let's say I have NamespaceA and NamespaceB (rough example below), how can I "embind" the methods in these namespaces to their own JS objects inside the main binding?

// Example namespace A
#include "myheaderfile_a.h"

uint8_t NamespaceA::functionA(uint8_t a, uint8_t b) {
 return a+b;
}

uint8_t NamespaceA::functionB(uint8_t a, uint8_t b) {
 return a+b*2;
}

uint8_t NamespaceA::functionC(uint8_t a, uint8_t b) {
 return b - a;
}

// Example namespace B
#include "myheaderfile_b.h"

uint8_t NamespaceB::functionA(uint8_t a, uint8_t b) {
 return a+b;
}

uint8_t NamespaceB::functionB(uint8_t a, uint8_t b) {
 return a+b*2;
}

uint8_t NamespaceB::functionC(uint8_t a, uint8_t b) {
 return b - a;
}

I'm looking for a usage structure as follows in Javascript:

const MyModule = require("binding.js");
MyModule.addOnPostRun(() => {
  console.log(MyModule);
  
  // NAMESPACE A
  MyModule.NamespaceA.functionA(6, 4);
  MyModule.NamespaceA.functionB(6, 4);
  MyModule.NamespaceA.functionC(6, 4);

  // NAMESPACE B
  MyModule.NamespaceB.functionA(6, 4);
  MyModule.NamespaceB.functionB(6, 4);
  MyModule.NamespaceB.functionC(6, 4);
});

How would I achieve this?

I tried exporting a value_object but it seems I'm not able to use a namespace as a type. Even then, I don't think it's right to try export a function as a field:

EMSCRIPTEN_BINDINGS()
{

    value_object<NamespaceA>("NamespaceA") 
       .field("functionA", &NamespaceA::functionA)
       .field("functionB", &NamespaceA::functionB)
       .field("functionC", &NamespaceA::functionC)
};
about 4 years ago · Juan Pablo Isaza
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!