Comments (7)
Sorry for the delayed response. To solve this issue, you need to make changes in the Rust part. You can either fork this repository or create a new one that references this Rust library. In this way, Rust (Wasm) can retain the execution result from JavaScript, and then you can retrieve the saved result from Rust (Wasm) in C++ using Wasmedge.
from wasmedge-quickjs.
If you don't want to use WebAssembly and prefer to directly run JavaScript with C++, then this library is not suitable.
But if you want to run WebAssembly through C++, and execute some JavaScript inside WebAssembly, then this library is somewhat suitable.
from wasmedge-quickjs.
@L-jasmine I want to run WebAssembly through C++, and execute some JavaScript inside WebAssembly.
I see here https://github.com/second-state/wasmedge-quickjs/blob/main/examples/embed_js/src/main.rs how to use it through Rust app.
What I want to understand is re how to use this lib through C++
from wasmedge-quickjs.
You can use the Wasmedge CPP API to run Wasmedge inside your C++ program, and then use Wasmedge to load the WebAssembly from the 'release' page.
Your C++ program will not directly use this library.
from wasmedge-quickjs.
@L-jasmine Here is what I'm trying currently to do:
#include <iostream>
#include <wasmedge/wasmedge.h>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
int runJS() {
/* Create the configure context and add the WASI support. */
/* This step is not necessary unless you need WASI support. */
WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
WasmEdge_ConfigureAddHostRegistration(ConfCxt,
WasmEdge_HostRegistration_Wasi);
/* The configure and store context to the VM creation can be NULL. */
WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
// /* The parameters and returns arrays. */
WasmEdge_Value Params[2] = {WasmEdge_ValueGenI32(5), WasmEdge_ValueGenI32(5)};
WasmEdge_Value Returns[1];
// /* Function name. */
WasmEdge_String FuncName = WasmEdge_StringCreateByCString("main");
/* Result. */
WasmEdge_Result Res;
/* Step 1: Load WASM file. */
Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "wasmedge_quickjs.wasm");
/*
* Developers can load the WASM binary from buffer with the
* `WasmEdge_VMLoadWasmFromBuffer()` API, or from
* `WasmEdge_ASTModuleContext` object with the
* `WasmEdge_VMLoadWasmFromASTModule()` API.
*/
if (!WasmEdge_ResultOK(Res)) {
printf("Loading phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
return 1;
}
/* Step 2: Validate the WASM module. */
Res = WasmEdge_VMValidate(VMCxt);
if (!WasmEdge_ResultOK(Res)) {
printf("Validation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
return 1;
}
/* Step 3: Instantiate the WASM module. */
Res = WasmEdge_VMInstantiate(VMCxt);
/*
* Developers can load, validate, and instantiate another WASM module to
* replace the instantiated one. In this case, the old module will be
* cleared, but the registered modules are still kept.
*/
if (!WasmEdge_ResultOK(Res)) {
printf("Instantiation phase failed: %s\n",
WasmEdge_ResultGetMessage(Res));
return 1;
}
/*
* Step 4: Execute WASM functions. You can execute functions repeatedly
* after instantiation.
*/
Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
if (WasmEdge_ResultOK(Res)) {
printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
} else {
printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
}
/* Resources deallocations. */
WasmEdge_VMDelete(VMCxt);
WasmEdge_ConfigureDelete(ConfCxt);
WasmEdge_StringDelete(FuncName);
return 0;
}
int main(int argc, char **argv) {
runJS();
return 0;
}
I load wasmedge-quickjs wasm file wasmedge_quickjs.wasm
and then trying to run there a main
function.
Then run it this way:
gcc main.cpp -lwasmedge && ./a.out
and it prints some result Get the result: 39894674
My current issue is the following:
- the
main
function accepts 2 parameters of I32, but I want to pass there a JS file name and args, simialar to CLI example$ wasmedge --dir .:. wasmedge_quickjs.wasm example_js/hello.js WasmEdge Runtime
Could you help me with this?
from wasmedge-quickjs.
The arguments for the main function in WebAssembly are obtained from WASI, so you need to set the arguments you want to pass to the main function in the WASI module, just like what was done in the link provided above.
Another point is that, for WebAssembly supporting WASI, their startup function is often _start
instead of main
. This is a function with no parameters and no return.
So your RunJs() should be modified to
int runJS() {
/* Create the configure context and add the WASI support. */
/* This step is not necessary unless you need WASI support. */
WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
WasmEdge_ConfigureAddHostRegistration(ConfCxt,
WasmEdge_HostRegistration_Wasi);
/* The configure and store context to the VM creation can be NULL. */
WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
// /* The parameters and returns arrays. */
WasmEdge_Value Params[0] = {}; //maybe, I am not very familiar with C++ syntax.
WasmEdge_Value Returns[0] = {}; //maybe, I am not very familiar with C++ syntax.
// /* Function name. */
WasmEdge_String FuncName = WasmEdge_StringCreateByCString("_start");
/* Result. */
WasmEdge_Result Res;
.....
from wasmedge-quickjs.
@L-jasmine that does work quite well, thanks a lot for your support 🥇
Just one final thing
My js file looks like the following:
import * as os from 'os';
import * as std from 'std';
import * as process from 'process'
args = args.slice(1);
print('echo Hello: ', ...args);
Is there a way to get this output after calling WasmEdge_VMExecute
and store into C++ variable?
from wasmedge-quickjs.
Related Issues (20)
- README example does not work HOT 4
- How to embed wasmedge-quickjs in a Node.js app? HOT 2
- Execute the add.js example HOT 1
- HTTPS support HOT 1
- Error running `react_ssr_stream` example HOT 2
- How to import a third-party nodejs library HOT 1
- README example run failed
- What is the quickjs source code? HOT 3
- Support for TLS-Less Builds? HOT 7
- process.argv undefined HOT 2
- Build without SIMD HOT 9
- The symbol "Stats" has already been declared HOT 9
- QuickJS license compliance HOT 2
- How to progress promises HOT 3
- Update QuickJS to latest, major bugs HOT 3
- Error stack traces HOT 5
- Get should be GET
- ServerResponse must have finished property
- fs.FileHandle is not defined or not a function
- opts is not an object HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasmedge-quickjs.