11

From this, I found out that JavaScript is written in C++. I also have found out/deduced that a majority of JavaScript is C++ (e.g. Math.atan+"" and Math.atan.toString() yielding "function atan() { [native code] }"). The [native code I assume to be C++, else what would be the point of 'hiding' it?

My question is there a way to utilize C++ in JavaScript? To use it in a function or in the JavaScript platform?

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
Conor O'Brien
  • 987
  • 2
  • 16
  • 40
  • 1
    The term "utilize" is such a loosely defined one. It could mean lots of different things. – Galik Nov 29 '14 at 03:12
  • 6
    Not all implementations of Javascript are written in C++. – Captain Obvlious Nov 29 '14 at 03:13
  • For example there are multiple implementations of JavaScript in Microsoft .Net Framework (including original one that is part of framework starting 1.0) - so you can write your code in JavaScript and use any of other language supported by .Net to implement other parts callable from JS... Or JavaScript used for scripting in Windows can use COM objects written in any language... But I strongly suspect it is not what you are asking for... – Alexei Levenkov Nov 29 '14 at 03:29
  • 1
    I think it is a better question if you stick to C and C++, opening up to any language would seem to make it too broad to me. – Shafik Yaghmour Nov 29 '14 at 03:34
  • https://github.com/zlatnaspirala/c-cpp-to-javascript – Nikola Lukic Oct 18 '19 at 09:29

3 Answers3

11

The emscripten project allows you to to generate Javascript from C and C++:

Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode - which can be generated from C/C++, using llvm-gcc (DragonEgg) or clang, or any other language that can be converted into LLVM - and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

and through methods like ccall and cwrap you can call C functions:

Using the example from the site, this C++ code which used extern "C" to prevent name mangling:

#include <math.h>

extern "C" {

int int_sqrt(int x) {
  return sqrt(x);
}

}

can be compiled like so:

./emcc tests/hello_function.cpp -o function.html -s EXPORTED_FUNCTIONS="['_int_sqrt']"

and used in Javascript:

int_sqrt = Module.cwrap('int_sqrt', 'number', ['number'])
int_sqrt(12)
int_sqrt(28)

embind can be used for C++ functions and classes. The quick example from the site is as follows:

// quick_example.cpp
#include <emscripten/bind.h>

using namespace emscripten;

float lerp(float a, float b, float t) {
    return (1 - t) * a + t * b;
}

EMSCRIPTEN_BINDINGS(my_module) {
    function("lerp", &lerp);
}

and compile:

emcc --bind -o quick_example.js quick_example.cpp

and use in Javascript:

<!doctype html>
<html>
  <script src="quick_example.js"></script>
  <script>
    console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
  </script>
</html>
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
6

WCPP is a package that lets you import C++ nearly directly into your Node project. Disclaimer: I am the maintainer of this project.

Our C++

// addTwo.cpp 

export int addTwo(int a, int b) {
  return a + b;
}

In the terminal

$ wcpp

Our JavaScript

const ourModule = await require('wcpp')('./addTwo.cpp')

console.log(ourModule.addTwo(2, 3))

For more information, see the NPM Package or the Git Repo

Brandon Dyer
  • 1,316
  • 12
  • 21
2

You can use NACL. It's a native client for chrome, but it's experimental. You have to write C++ code and then make references to it in your JS files.

https://developer.chrome.com/native-client/overview

Patema
  • 31
  • 2