I needed to extend my C app with a scripting language so I tried MuJS, it's written in simple C, but it lacks documentation about specifics. Digging through the code I couldn't figure out how to do this basic task of creating a simple object with a function inside it.
expected result:
myobject: {
myfn: function () {}
}
I tried this but it resulted in an "uncaught exception".
js_newobject(J);
{
js_newcfunction(J, myfn, "myfn", 2);
js_setproperty(J, -2, "myfn");
}
js_setglobal(J, "myobject");
As you mentionned it, MuJS is poorly documented. You must consider switching to a better documented and widely used alternatives such as LuaJIT. Look at an example here .
You're probably doing something else wrong, because the code you wrote should work as expected.
Here's a full example with your code that compiles and runs without errors:
#include <stdio.h>
#include "mujs.h"
void myfn(js_State *J)
{
printf("Hello, world!\n");
}
int main(int argc, char **argv)
{
js_State *J = js_newstate(NULL, NULL, 0);
js_newobject(J);
js_newcfunction(J, myfn, "myfn", 0);
js_setproperty(J, -2, "myfn");
js_setglobal(J, "myobject");
js_dostring(J, "myobject.myfn()");
js_freestate(J);
}
Related
I'm trying to embed an custom function to my project, that uses the V8 engine, and apparently I can't make it working. I've used code, that I've found, but it seems to be outdated, or I just do it wrong. My point is to include a custom javascript file. My current code (for testing) is this :
HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
global->Set(v8::String::NewFromUtf8(isolate, "test", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, test));
Handle<Context> context = Context::New(isolate);
Persistent<Context> persistent_context(isolate, context);
Context::Scope context_scope(context);
const char* data = "test";
Handle<String> source = String::NewFromUtf8(isolate, data);
Handle<Script> script = Script::Compile(source);
if (!script.IsEmpty())
Handle<Value> result = script->Run();
Test Code (obviously just for testing):
void test(const v8::FunctionCallbackInfo<v8::Value>& args) {
MessageBoxA(NULL,"test", "", 0);
}
But the engine returns this error :
Uncaught ReferenceError: test is not defined
So my question is if I even do it correct, I would be able to make the including myself I hope, but I just can't get the function to get executed.
Here's some code from a project that works:
Isolate::Scope iscope( isolate_ );
HandleScope hs( isolate_ );
Local<Object> test = Object::New(isolate_);
test->Set(String::NewFromUtf8(isolate_, "javaScriptFunctionName"), Function::New(isolate_, CPP_FN_CALLBACK));
global_template->Set(String::NewFromUtf8(isolate_, "test"), test);
That will result in an object for window.test with a function called window.test.javaScriptFunctionName()
Dynamic library is created using some C files. This dynamic library is used by a C++ function. One of the C-functions calls a C++ function. Which in turn will invoke a Javascript Function Callback. I am getting segmentation fault in C++ function.
Here is the code,
In test.h,
#ifdef __cplusplus
extern "C" {
#endif
void cppFunc(void);
#ifdef __cplusplus
}
#endif
Here is the code in test.c
#include "test.h"
void cFunc(void){
/* some code */
cppFunc();
}
Here is the code in test.cpp
#include "test.h"
static Nan::Persistent<v8::Function> callback;
void storeFunc(const v8::FunctionCallbackInfo<v8::Value>& args){
if(args[0]->IsFunction()){
Local<Function> cb = Local<Function>::Cast(args[0]);
callback.Reset(cb);
}
}
void cppFunc(void){
Isolate *isolate = Isolate::GetCurrent();
Local<Function> c_back = Local<Function>::New(isolate, callback);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), c_back, 0, {});
}
void init(Handle<Object> exports, Handle<Object> module)
{
NODE_SET_METHOD(exports, "FuncStore", storeFunc);
}
NODE_MODULE(test, init)
test.node is created by using "node-gyp configure build" command. No errors or warnings are shown.
Following is my test.js file,
var sample = require("./build/Release/test.node");
sample.FuncStore(function(){
console.log("Callback has been called!!");
});
On executing test.js by "node test.js", a segmentation fault occurs when cFunc() calls cppFunc(). Segmentation fault is happening exactly at the line "Local c_back = Local::New(isolate, callback);" in cppFunc().
What could be the reason?
One possibility is that the pointer "isolate" is NULL. Accessing a pointer with no value or an invalid value can cause a segmentation fault. You should check for it's existence by performing:
Isolate *isolate = Isolate::GetCurrent();
if (isolate) {
//it is now safe to use isolate
Local<Function> c_back = Local<Function>::New(isolate, callback);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), c_back, 0, {});
}
else {
//possibly generate your own warning here
}
As a side note, it seems Isolate::GetCurrent is deprecated https://github.com/nodejs/node/commit/409d413363
I have a C++ method (which role is killing some processes). She needs 2 parameters : a hostname and a port.
On the other hand, I am developing a web-application (using Nodejs and AngularJS), running on Google Chrome.
When I click on a button through the browser, I would like to be able to call the C++ function, through my app.js file.
I haven't found how to "bind" javascript with C++.
EDIT : I think this link could be very useful
How can I use a C++ library from node.js?
You can use Google's V8. V8 is Google's open source JavaScript engine.
V8 can run standalone, or can be embedded into any C++ application.
http://code.google.com/p/v8/
Following example from github demonstrates, binding a C++ class with Google V8.
v8_wrap_class.cpp - Author is nicholas
/*
* v8_wrap_class.cpp
*
* Created on: 14/01/2013
* Author: nicholas
* License: public domain
*/
#include <v8.h>
#include <cstdio>
#include <string>
#include <stdexcept>
#include <memory>
using namespace v8;
/*
var Simple = function(v)
{
this.value = v;
}
Simple.prototype.func = function()
{
alert(this.value);
}
var obj = new Simple(4);
obj.func();
*/
struct Simple
{
double value;
Simple(double v)
: value(v)
{
fprintf(stderr, "Simple::ctor\n");
}
void func()
{
fprintf(stderr, "Simple::func(%f)\n", value);
}
~Simple()
{
fprintf(stderr, "Simple::dtor\n");
}
};
namespace js
{
/*
* Retrieve the c++ object pointer from the js object
*/
template <typename T>
T* unwrap(const Arguments& args)
{
auto self = args.Holder();
auto wrap = Local<External>::Cast(self->GetInternalField(0));
return static_cast<T*>(wrap->Value());
}
/*
* Construct a new c++ object and wrap it in a js object
*/
template <typename T, typename... Args>
Persistent<Object> make_object(Handle<Object> object, Args&&... args)
{
auto x = new T(std::forward<Args>(args)...);
auto obj = Persistent<Object>::New(object);
obj->SetInternalField(0, External::New(x));
obj.MakeWeak(x, [](Persistent<Value> obj, void* data)
{
auto x = static_cast<T*>(data);
delete x;
obj.Dispose();
obj.Clear();
});
return obj;
}
}
void bind_Simple(Local<Object> global)
{
// Name the class in js
auto name = String::NewSymbol("Simple");
auto tpl = FunctionTemplate::New([&](const Arguments& args) -> Handle<Value>
{
if (!args.IsConstructCall())
return ThrowException(String::New("Cannot call constructor as function"));
HandleScope scope;
// Read and pass constructor arguments
js::make_object<Simple>(args.This(), args[0]->NumberValue());
return args.This();
});
tpl->SetClassName(name);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
auto prototype = tpl->PrototypeTemplate();
// Add object properties to the prototype
// Methods, Properties, etc.
prototype->Set(String::New("func"), FunctionTemplate::New([](const Arguments& args) -> Handle<Value>
{
auto s = js::unwrap<Simple>(args);
s->func();
return {};
})->GetFunction());
auto constructor = Persistent<Function>::New(tpl->GetFunction());
global->Set(name, constructor);
}
int main()
{
std::string js_source = R"(
for(var i = 0; i < 1000; ++i)
{
var s = new Simple(4);
s.value = 5;
s.func();
}
)";
/*
* This code is mostly uninteresting.
* Just run the vm with the script provided.
*/
{
HandleScope handle_scope;
Handle<ObjectTemplate> global_template = ObjectTemplate::New();
Persistent<Context> context = Context::New(0, global_template);
Context::Scope context_scope(context);
auto global = context->Global();
// Wrap the class and bind to the global scope.
bind_Simple(global);
{
HandleScope handle_scope;
TryCatch trycatch;
Local<String> source = String::New(js_source.c_str(), js_source.size());
Local<Script> script = Script::Compile(source);
if (script.IsEmpty())
{
Handle<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::runtime_error(*exception_str);
}
Local<Value> result = script->Run();
if (result.IsEmpty())
{
Local<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::runtime_error(*exception_str);
}
}
context.Dispose();
context.Clear();
}
// Run the GC until there is nothing to reclaim.
while (!V8::IdleNotification())
;
return 0;
}
This answer gives four appraoches to using C++ in javascript. The methods shown try to keep the original C++ in a standard and simple C++ implementation.
Two methods using WASM and two using SWIG and JRPC are used to give examples and ideas on executing C++ in javascript. In detail, this answer gives one example for executing in the browser and one for executing in nodejs using WASM. The end of this answer also lists two other ways to execute C++ in javascript, one of which calls nodejs from the browser which is slightly more complex but possible using jrpc-oo.
If you want to execute in the browser or nodejs, then you can compile your C++ to WASM and load that module in the browser or nodejs, executing the C++ there. This WASM repo exemplifies how to do that. I will expand the key code in the WASM repo here.
Create some C++ code and declare your WASM binding, for example (from the files include/Test.H and src/Test.C) :
class Test {
public:
void sayHello(){
printf("Hi, my name is test\n");
}
};
#include <emscripten/bind.h>
EMSCRIPTEN_BINDINGS(Test_ex) {
emscripten::class_<Test>("Test")
.function("sayHello", &Test::sayHello)
;
}
Compile that down and you can now run that in nodejs (from the file nodejs/WASMTestNode.js) :
libWASM = require('./libwasmNode.js');
libWASM().then((mod)=>{
libWASM = mod;
let test = new libWASM.Test;
test.sayHello();
});
In the browser you can use the WASM code as well. To do that firs import the WASM library (from the file webcomponent/libWASM.js) :
import modProm from './libwasm.js';
The create your webcomponent and compile your WASM then execute the C++ method Test::sayHello (from the file webcomponent/libWASM.js) :
import { LitElement } from 'lit';
export class LibWASM extends LitElement {
constructor() {
super();
modProm().then((mod)=>{
this.libWASM = mod; // for rendered wasm that delay
this.WASMReady();
})
}
WASMReady(){
console.log('LibWASM.libWASM module compiled and ready to go.')
let test = new this.libWASM.Test;
test.sayHello();
}
}
This code example is implemented in the reference repo.
Alternatively a third way to do this is to only use C++, SWIG and nodejs from this reference repo.
If you want to execute nodejs from the browser or use a a different method of integrating C++ into nodejs, you can also look at this SWIG and jrpc-oo reference for doing the same thing, but not only in nodejs, also from the browser calling nodejs.
There are also other ways to execute C++ form javascript, however the methods demonstrated in this answer are reasonably straightforward and either rely on WASM binding or SWIG abstraction which leaves your original code as standard C++.
I am learning about emscripten and trying to understand it better. As far as I understand the use-case it was mostly designed for is to port existing C/C++-code to a web client (browser) and calling C/C++ code from JavaScript.
But I am wondering whether it is possible to use C++ and Emscripten to web page (note: this is more out of curiosity - I know that there are not many good reasons to do that at the moment). I manage to call Javascript functions from C++ and pass arguments of types string, int, double etc to them. But what I am missing is: calling a Javascript function from C++ and passing a C or C++ function as a handle. So as a simple example: How would I write the following Javascript code ind pure C++?
var myfun = function() { /* do something meaningful here */ }
document.onload(myfun);
TL;DR;
I wrote a library : js-bind which accepts any number of arguments to do that easily :
using namespace std::placeholders;
using emscripten::val;
// First find the HTML object to attach the event to
auto clickme_btn = val::global("document").call<val>("getElementById", string("clickme_btn"));
// Bind the event handler for click
auto onclick = [](val event){ cout << "hello world ! " << endl; };
clickme_btn.set("onclick", js::bind(onclick, _1));
This library is some Macro metaprogramming based on the explanation below.
DETAILED ANSWER:
You have different possibilites, like emscripten ccall, but what is easier to use in my opinion is Embind.
For example take binding event handlers of an XMLHttpRequest from within C++.
To enable it you have to compile with : --bind -s NO_EXIT_RUNTIME=1
Emscripten : bind freestanding functions
One could achieve it easily with freestanding functions and a singleton, as show here :
#include <iostream>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
namespace xhr {
inline emscripten::val& singleton() {
using emscripten::val;
static val instance = val::global("XMLHttpRequest").new_();
return instance;
}
void on_load(emscripten::val event) {
std::cout << "Successful Query " << std::endl;
std::cout << "response is : " << singleton()["responseText"].as<std::string>() << std::endl;
}
void on_error(emscripten::val event) {
std::cout << "Error on query " << std::endl;
}
void on_progress(emscripten::val event) {
std::cout << "Progress on query " << std::endl;
std::cout << event["lengthComputable"].as<bool>() << ": " << event["loaded"].as<unsigned int>() / event["total"].as<unsigned int>() << std::endl;
}
using namespace emscripten;
EMSCRIPTEN_BINDINGS(xhr) {
function("on_load", &on_load);
function("on_error", &on_error);
function("on_progress", &on_progress);
}
}
int main(int argc, char** argv) {
using emscripten::val;
xhr::singleton().call<val>("open", std::string("GET"), std::string("http://127.0.0.1:8080/CMakeCache.txt"), true);
// Here I set the callback to &on_load function registered via the EMSCRIPTEN_BINDINGS macro.
xhr::singleton().set("onload",val::module_property("on_load"));
xhr::singleton().set("onprogress",val::module_property("on_progress"));
xhr::singleton().set("onerror",val::module_property("on_error"));
xhr::singleton().call<val>("send");
return 0;
}
Emscripten : bind member functions
Typically in C++ we are used to std::bind callbacks. This can also be achieved, taking the example of xhr in a cleaner way :
#include <iostream>
#include <functional>
#include <memory>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
class MiniXhr : public std::enable_shared_from_this<MiniXhr> {
using val = emscripten::val;
using url_t = std::string;
public:
void set_url(const url_t& url) { url_ = url; }
void GET();
/**
*
* The member function to be called from javascript.
*/
void on_readystate(val event) {
std::cout << "ready " << std::endl;
std::cout << "xxhr::on_readystate: "
<< xhr["readyState"].as<size_t>() << " - " << url_ << " :: "
<< xhr["status"].as<size_t>() << ": "
<< xhr["statusText"].as<std::string>() << std::endl;
}
private:
url_t url_;
val xhr = val::global("XMLHttpRequest").new_();
};
using emscripten::class_;
EMSCRIPTEN_BINDINGS(MiniXhr) {
/**
* Binding for the class.
*/
class_<MiniXhr>("MiniXhr")
.smart_ptr<std::shared_ptr<MiniXhr>>("shared_ptr<MiniXhr>")
.function("on_readystate", &MiniXhr::on_readystate)
;
/**
* More generic binding to bind a functor with one argument (event handler get the event)
* Here std::function call operator from C++ is bound to function opcall() in JS.
*/
class_<std::function<void(emscripten::val)>>("VoidValFunctor")
.constructor<>()
.function("opcall", &std::function<void(emscripten::val)>::operator());
}
/**
*
* Finally the interesting part : binding the member function on_readystate to the readystatechange event of XMLHttpRequest.
*
*/
void MiniXhr::GET() {
/**
* Here this lambda could be put as function in a library, to do an JS(std::bind),
* it should just be overloaded for different argument count. (Im on it).
*/
auto jsbind = [](val& target, const char* property, auto bind_expression ) {
// Create an std::function from the bind expression
std::function<void(emscripten::val)> functor = bind_expression;
// We ensure the correct object will always be bound to the this of the function
auto functor_adapter = val(functor)["opcall"].call<val>("bind", val(functor));
// Finally we simply set the eventhandler
target.set(property, functor_adapter);
};
// Here we could bind as many member function as we want.
// jsbind(xhr, "onload", std::bind(&MiniXhr::on_load, shared_from_this(), std::placeholders::_1));
// jsbind(xhr, "onerror", std::bind(&MiniXhr::on_error, shared_from_this(), std::placeholders::_1));
// jsbind(xhr, "onprogress", std::bind(&MiniXhr::on_progress, shared_from_this(), std::placeholders::_1));
jsbind(xhr, "onreadystatechange", std::bind(&MiniXhr::on_readystate, shared_from_this(), std::placeholders::_1));
// Note that we bind with shared_from_this(), as the scope where the class was instantiated may be dead
// and only later our callback will come back.
xhr.call<val>("open", std::string("GET"), url_, true);
xhr.call<val>("send");
}
int main(int argc, char** argv) {
auto x = std::make_shared<MiniXhr>();
x->set_url("notfound.json");
x->GET();
return 0;
}
Here's something I used a long while back when tinkering w/ Emscripten in C code:
void myfun(void(*f)(void)) { (*f)() }
and then here would be the JavaScript:
var theparty = Runtime.addFunction(function() { print("Will there be confetti?") });
Module.ccall("myfun", "number", ["number"], [theparty]);
Runtime.removeFunction(theparty); // output => Will there be confetti?
I always remove a function that is no longer needed after its execution to preserve memory.
This is a simple and seamless way to make code bits work together. You can obviously modify this to do whatever you want other than printing out information. :P
I'm not sure about emscripten, but to sum up, I understand that what you need to know is how to pass a C++ function as a handle to another C++ function. I hope I can help with that.
JavaScript, PHP, and other more flexible languages, allow a function object to be passed through. In C and C++, it is slightly different, you have to pass function pointers as arguments to other functions. In C, the name for this is a Callback, rather than a handle.
For instance:
/* This function takes a single callback as a parameter. */
//here we say that the parameter, that we name numberSource, is a function that receives no parameters itself (void), and return an int
void printNumber(int (*numberSource)(void)) {
printf("%d", numberSource());
}
/* A possible callback */
int oneExampleFunction(void) {
return 100;
}
/* Another possible callback. */
int otherExampleFunction(void) {
return 200;
}
/* This is how we would call printNumber with three different callbacks. */
//with "&" we are referencing the memory address of the function,
//since thats what printNumber is expecting
printNumber(&oneExampleFunction);
printNumber(&otherExampleFunction);
printNumber(&rand); //where are using rand(), a system function, that works as well.
It is a common practice to create a custom type to the argument, so you don't need to use something as ugly as int (*numberSource)(void). It will be something like:
//Function pointer called CallbackType that takes a float
//and returns an int
typedef int (*NameYouWantForTheType)(void);
So the printNumber function would be like this:
void printNumber(NameYouWantForTheType numberSource ) {
printf("%d", numberSource());
}
So, in your case,
if you want to translate this JS code
var myfun = function() { /* do something meaningful here */ }
document.onload(myfun);
to C, and you have a C object called "document" that receives a function that performs some other actions, your C code will be:
void myfun (void) {
/* do something meaningful here */
}
document.onload(&myfun);
I have a piece of C++ code converted to JavaScript via Emscripten. I would like the converted C++ code to call back to the JavaScript code that calls it. Something like:
JavaScript:
function callback(message) {
alert(message);
}
ccall("my_c_function", ..., callback);
C++:
void my_c_function(whatever_type_t *callback) {
callback("Hello World!");
}
Is this possible somehow?
I believe the accepted answer is a bit outdated.
Please refer to this bullet point in the "Interacting with code" emscripten tutorial.
E.g.
C:
void invoke_function_pointer(void(*f)(void)) {
(*f)();
}
JS:
var pointer = Runtime.addFunction(function() {
console.log('I was called from C world!');
});
Module.ccall('invoke_function_pointer', 'number', ['number'], [pointer]);
Runtime.removeFunction(pointer);
This way the C-code does not need to be aware of that it is transpiled to JS and any bridges required can purely be controlled from JS.
(code hacked into message composer; may contain errors)
There is a new way of achieving your requirement which is via embind.
Consider the following piece of C++ code.
#include <emscripten/bind.h>
using namespace emscripten;
void cbTest(emscripten::val cb)
{
cb();
}
EMSCRIPTEN_BINDINGS(my_module) {
function("cbTest", &cbTest);
}
The cbTest C++ function takes in a emscripten::val. This can be an object of any kind. For us this is a function object.
This is how you will call it from JS
var cbFunc = function() {
console.log("Hi, this is a cb");
}
Module.cbTest(cbFunc);
P.S This api is still under construction.
A thing that is frequently done in Emscripten is to map strong types to simple ones.
JS:
function callback(message) {
alert(message);
}
var func_map = {
0: callback
};
// C/C++ functions get a _ prefix added
function _invoke_callback(callback_id, text_ptr) {
func_map[callback_id](Pointer_stringify(text_ptr));
}
ccall("my_c_function", ..., 0);
C++:
// In C/C++ you only need to declare the func signature and
// make sure C is used to prevent name mangling
extern "C" void invoke_callback(int callback_id, const char* text);
void my_c_function(int callback_id) {
invoke_callback( callback_id, "Hello World!" );
}
And of course, you can add some glue code, so this gets very seamless.
I needed to write something very similar to what is described in the question. My code ended up looking like this:
C:
void call(void (*back)(char*)){
back("Hello!");
}
JS:
function back(text){
alert(Pointer_stringify(text));
}
var pointer = Runtime.addFunction(back);
var call = Module.cwrap('call', 'void', ['pointer']);
call(pointer);
Runtime.removeFunction(pointer);
Note that the pointer returned to the callback has to be dereferenced with Pointer_stringify.
You can find example code like this on GitHub.
Here's what I have gathered from several posts and by looking at Emscripten bundled code:
In C++:
#include <iostream>
#include <functional>
extern "C" {
void registerCallback(void(*back)(const char*));
void triggerCallback(char* message); // for invoking it from JS, just for this example
}
// global
std::function<void(const char*)> gCallback;
void registerCallback(void(*back)(const char*)){
gCallback = back;
}
void triggerCallback(char* message){
if (gCallback) {
gCallback(message);
} else {
std::cerr << "Cannot pass '"<< message <<"' to undefined callback\n";
}
}
An important thing, which was missing in other posts, is to compile C++ with RESERVED_FUNCTION_POINTERS=... flag, e.g.:
em++ -std=c++11 -s RESERVED_FUNCTION_POINTERS=20 source.cpp -s EXPORTED_FUNCTIONS="['_registerCallback','_triggerCallback']" -o try.html
After loading try.html into a browser, you can execute the following JS code in its console:
// Register a callback function
function callback(text){ alert("In JS: "+Pointer_stringify(text)); }
var cb = Runtime.addFunction(callback);
_registerCallback(cb);
// Invoke it with some "C string"
var jsStr = "XOXOXO";
var cStr = allocate(intArrayFromString(jsStr), 'i8', ALLOC_NORMAL)
_triggerCallback(cStr);
// Free Emscripten heap and release the function pointer
_free(cStr);
Runtime.removeFunction(cb);
You should see an alert with "In JS: XOXOXO".