Commit 2da51db3 authored by Boxiang Sun's avatar Boxiang Sun

Load and initialize pyodide python wasm module

parent 669d0fb4
/*global window, console, RSVP, document, URL, eval, XMLHttpRequest, marked */
/*global window, console, RSVP, document, URL, eval, XMLHttpRequest, marked, WebAssembly */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window) {
"use strict";
......@@ -11,6 +11,8 @@
this._line_list = line_list;
this._option = option;
},
is_pyodide_loaded = false,
pyodide_instance,
split_line_regex = /[\r\n|\n|\r]/,
cell_type_regexp = /^\%\% (\w+)/;
......@@ -68,7 +70,6 @@
var line_list = jsmd.split(split_line_regex),
i,
len = line_list.length,
code_type,
current_line,
current_type,
current_header,
......@@ -264,13 +265,84 @@
});
}
function loadPyodide(imports, success_callback) {
return new RSVP.Queue()
.push(function () {
return ajax({
url: "pyodide.asm.wasm",
dataType: "arraybuffer"
});
})
.push(function (evt) {
// Compile and instantiate WebAssembly code.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
return WebAssembly.instantiate(evt.target.response, imports);
})
.push(function (result) {
// This function act as instantiateWasm
// And in instantiateWasm, we have to call the second parameter
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
return success_callback(result.instance);
});
}
function checkABI(ABI_number) {
// This was needed by pyodide wasm module
if (ABI_number !== 1) {
throw new Error("Expect ABI number 1, got " + ABI_number);
}
return true;
}
function initPyodide() {
var Module = {};
// perform the WebAssembly instantiation action
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
Module.instantiateWasm = loadPyodide;
Module.checkABI = checkABI;
window.Module = Module;
return RSVP.Queue()
.push(function () {
return loadJSResource('pyodide.asm.data.js');
})
.push(function () {
return loadJSResource('pyodide.asm.js');
})
.push(function () {
// pyodide is a function defined in pyodide.asm.js
// which set the functions under Module
pyodide_instance = window.pyodide(Module);
var defer = RSVP.defer();
// define postRun to resolve the promise
// When pyodide loading completed, it will call Module.postRun
// to signal the module loaded and ready for next step
// https://github.com/emscripten-core/emscripten/blob/1.38.10/src/preamble.js#L1602
// https://emscripten.org/docs/api_reference/preamble.js.html
Module.postRun = defer.resolve;
return defer.promise;
});
}
function executePyCell(line_list) {
var result, code_text = line_list.join('\n');
result = pyodide_instance.runPython(code_text);
}
function executeCodeCell(line_list, option) {
if (option.language === 'py') {
console.log(line_list);
console.log("We are processing Python code!!!");
} else {
if (option.language !== 'py') {
throw new Error('Unsupported code language: ' + option.language);
}
if (!is_pyodide_loaded) {
is_pyodide_loaded = true;
return initPyodide()
.push(function () {
return executePyCell(line_list);
});
}
return executePyCell(line_list);
}
function executeCell(cell) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment