Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Boxiang Sun
erp5
Commits
2da51db3
Commit
2da51db3
authored
Jul 23, 2019
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Load and initialize pyodide python wasm module
parent
669d0fb4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
6 deletions
+78
-6
bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
...ateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
+78
-6
No files found.
bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
View file @
2da51db3
/*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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment