Skip to content

Commit

Permalink
first sane version
Browse files Browse the repository at this point in the history
  • Loading branch information
DerThorsten committed Jan 22, 2024
1 parent a750f94 commit cc02b1c
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ build/
build_wasm/

bld

# macOS
.DS_Store
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ if(EMSCRIPTEN)
PUBLIC "SHELL: -s ASYNCIFY"
PUBLIC "SHELL: -s FORCE_FILESYSTEM=1"
PUBLIC "SHELL: --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src/pre.js"
PUBLIC "SHELL: --post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/post.js"
)
endif()

Expand Down
65 changes: 62 additions & 3 deletions notebooks/xeus-javascript.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
"# Simple code execution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -108,8 +119,7 @@
"metadata": {},
"source": [
"# Scope\n",
"all variables declared via `const` or `let` are scoped to the cell they are declared in and are not accessible from other cells.\n",
"All variables declared via `var` are scoped to the notebook and are accessible from other cells."
"all toplevel variables (const/let/var) are accessible from other cells."
]
},
{
Expand All @@ -126,6 +136,17 @@
"let fubar_local=43;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -150,9 +171,47 @@
},
"outputs": [],
"source": [
"// this does not\n",
"// this also works\n",
"\n",
"console.log(fubar_local)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Const reassignment\n",
"within a cell, const cannot be reassigned, but it can be reassigned in another cell, or\n",
"when the cell is re-executed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"const some_const_var = 42;\n",
"pp(some_const_var)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"const some_const_var = 43;\n",
"pp(some_const_var)"
]
}
],
"metadata": {
Expand Down
2 changes: 2 additions & 0 deletions src/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Module["FS"] = FS;
107 changes: 80 additions & 27 deletions src/pre.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
Module["_make_async_from_code"] = function (code) {

let async_function = Function(`
const afunc = async function(){
const code_ast = Module["_ast_parse"](code);
let extra_code = [];
for(const node of code_ast.body){
if(node.type == "FunctionDeclaration")
{
const name = node.id.name;
extra_code.push(`globalThis[\'${name}\'] = eval(${name})`);
}
else if(node.type == "VariableDeclaration")
{
const name = node.declarations[0].id.name;
extra_code.push(`globalThis[\'${name}\'] = eval(${name})`);
}
}
const ec = extra_code.join('\n');
const total_code = code.concat('\n', ec);

function __storeVars(target) {
return new Proxy(target, {
has(target, prop) { return true; },
get(target, prop) {
if(prop in target){
return target[prop];
}
else{
if (typeof globalThis[prop] === 'function') {
return globalThis[prop].bind(globalThis);
}
return globalThis[prop];
}
}
});
}
let __stored_vars = {};

with(__storeVars(__stored_vars)) {
${code}
}
for (const [key, value] of Object.entries(__stored_vars)) {
globalThis[key] = value
}
let async_function = Function(`
const afunc = async function(){
${total_code}
}
return afunc;
`)();
return async_function;
}

Module["_ast_parse_options"] = {
ranges: true
};

Module["_ast_parse"] = function (code) {
return globalThis["meriyah"].parseScript(code, Module["_ast_parse_options"]);
}

Module["_configure"] = function () {
_clog = console.log;

console.log("import meriyah");
const url="https://cdn.jsdelivr.net/npm/meriyah@4.3.9/dist/meriyah.umd.min.js"
importScripts(url);



globalThis.ijs
globalThis.pprint = function(...args){
// stringify all with 2 spaces
// and join with space
// and add newline at the end

let msg = ""
for (let i = 0; i < args.length; i++) {
msg += `${JSON.stringify(args[i], null, 2)}`;
if (i < args.length - 1) {
msg += " ";
}
}
Module["_publish_stdout_stream"](`${msg}\n`);
}
// alias
globalThis.pp = globalThis.pprint;


console.log = function (... args) {
let msg = ""
for (let i = 0; i < args.length; i++) {
Expand All @@ -45,7 +72,6 @@ Module["_configure"] = function () {
}
Module["_publish_stdout_stream"](`${msg}\n`);
}
_cerr = console.error;
console.error = function (... args) {
let msg = ""
for (let i = 0; i < args.length; i++) {
Expand All @@ -62,6 +88,7 @@ Module["_configure"] = function () {
}

Module["_call_user_code"] = async function (code) {

try{
const async_function = Module["_make_async_from_code"](code);
await async_function();
Expand All @@ -70,13 +97,39 @@ Module["_call_user_code"] = async function (code) {
}
}
catch(err){

// if the error is an integer, then
// its a c++ exception ptr
// so we need to get the error message

if(typeof err === "number"){
console.log("catched c++ exception", err);
let msg = Module["get_exception_message"](err);

// if promise
if(msg instanceof Promise){
console.log("awaiting promise");
msg = await msg;
}


console.log("msg", msg);
return{
error_type: "C++ Exception",
error_message: `${msg}`,
error_stack: "",
has_error: true
}
}

return{
error_type: `${err.name || "UnkwownError"}`,
error_message: `${err.message || ""}`,
error_stack: `${err.stack || ""}`,
has_error: true
}
}

}


Expand Down
9 changes: 7 additions & 2 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// embind
#include <emscripten/bind.h>
#include <emscripten/emscripten.h>

#include <sstream>

Expand Down Expand Up @@ -51,6 +52,7 @@ namespace xeus_javascript

// std::cout<<"display_data"<<std::endl;


// publish stream
interpreter.display_data(
data["data"],
Expand All @@ -62,7 +64,7 @@ namespace xeus_javascript

interpreter::interpreter()
{
std::cout<<"V44"<<std::endl;
std::cout<<"Welcome to the 60th iteration of this file kernel (due to the service worker caching I need to print this to keep sanity)"<<std::endl;
xeus::register_interpreter(this);
}

Expand All @@ -75,6 +77,8 @@ namespace xeus_javascript
{
nl::json kernel_res;



auto result_promise = emscripten::val::module_property("_call_user_code")(code);
auto result = result_promise.await();
if(result["has_error"].as<bool>()) {
Expand All @@ -100,7 +104,8 @@ namespace xeus_javascript
}


return xeus::create_successful_reply(nl::json::array(), nl::json::object());

return xeus::create_successful_reply(nl::json::array(), nl::json::object());
}

void interpreter::configure_impl()
Expand Down

0 comments on commit cc02b1c

Please sign in to comment.