Skip to content

Commit 7eeaa90

Browse files
authored
Merge pull request #23 from Distributive-Network/wes/module-system
Merge wes/module-system - add node-flavoured CommonJS module support
2 parents cd0185a + 8445eee commit 7eeaa90

33 files changed

+1375
-21
lines changed

.gitignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
build/
22
.vscode/
33
pyvenv.cfg
4+
python/pythonmonkey/version.py
5+
python/pythonmonkey/node_modules
46
bin/
57
lib/*
68
.pytest_cache
79
.DS_Store
810
firefox-*.tar.xz
911
firefox-*/
10-
tests/__pycache__/*
11-
tests/python/__pycache__/*
12+
__pycache__
1213
Testing/Temporary
1314
_spidermonkey_install
14-
__pycache__
15+
__pycache__/*
1516
dist
1617
*.so
18+
_spidermonkey_install/*
19+
*~
1720
*.dylib
1821
*.dll
1922
*.pyd

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
5555
set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIRS})
5656
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
5757
message("Apple - Using Python:${Python_VERSION_MAJOR} - Libraries:${PYTHON_LIBRARIES} - IncludeDirs: ${PYTHON_INCLUDE_DIR}")
58-
elseif(LINUX)
58+
elseif(UNIX)
5959
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
6060
set(Python_FIND_VIRTUALENV FIRST) # (require cmake >= v3.15 and this is the default) use the Python version configured by pyenv if available
6161
set(PYTHON_LIBRARIES ${Python_LIBRARIES})

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
PythonMonkey is a Mozilla [SpiderMonkey](https://firefox-source-docs.mozilla.org/js/index.html) JavaScript engine embedded into the Python VM,
77
using the Python engine to provide the JS host environment.
88

9-
This product is in an early stage, approximately 65% to MVP as of March 2023. It is under active development by Distributive Corp.,
9+
This product is in an early stage, approximately 80% to MVP as of May 2023. It is under active development by Distributive Corp.,
1010
https://distributive.network/. External contributions and feedback are welcome and encouraged.
1111

1212
The goal is to make writing code in either JS or Python a developer preference, with libraries commonly used in either language
@@ -30,15 +30,15 @@ this package to execute our complex `dcp-client` library, which is written in JS
3030
- [done] JS functions coerce to Python function wrappers
3131
- [done] JS exceptions propagate to Python
3232
- [done] Implement `eval()` function in Python which accepts JS code and returns JS->Python coerced values
33-
- [underway] NodeJS+NPM-compatible CommonJS module system
33+
- [done] NodeJS+NPM-compatible CommonJS module system
3434
- [done] Python strings coerce to JS strings
3535
- [done] Python intrinsics coerce to JS intrinsics
36-
- Python dicts coerce to JS objects
37-
- Python `require` function, returns a coerced dict of module exports
36+
- [done] Python dicts coerce to JS objects
37+
- [done] Python `require` function, returns a coerced dict of module exports
3838
- [done] Python functions coerce to JS function wrappers
39-
- CommonJS module system .py loader, loads Python modules for use by JS
39+
- [done] CommonJS module system .py loader, loads Python modules for use by JS
4040
- JS object->Python dict coercion supports inherited-property lookup (via __getattribute__?)
41-
- Python host environment supplies event loop, including EventEmitter, setTimeout, etc.
41+
- [done] Python host environment supplies event loop, including EventEmitter, setTimeout, etc.
4242
- Python host environment supplies XMLHttpRequest (other project?)
4343
- Python host environment supplies basic subsets of NodeJS's fs, path, process, etc, modules; as-needed by dcp-client (other project?)
4444
- Python TypedArrays coerce to JS TypeArrays
@@ -53,6 +53,7 @@ this package to execute our complex `dcp-client` library, which is written in JS
5353
- rust
5454
- python3.8 or later with header files (python3-dev)
5555
- spidermonkey 102.2.0 or later
56+
- npm (nodejs)
5657
- [Poetry](https://python-poetry.org/docs/#installation)
5758
- [poetry-dynamic-versioning](https://github.com/mtkennerly/poetry-dynamic-versioning)
5859

@@ -95,3 +96,23 @@ Type "help", "copyright", "credits" or "license" for more information.
9596
```
9697

9798
Alternatively, you can build a `wheel` package by running `poetry build --format=wheel`, and install it by `pip install dist/*.whl`.
99+
100+
## Examples
101+
102+
* [examples/](examples/)
103+
* https://github.com/Distributive-Network/PythonMonkey-examples
104+
* https://github.com/Distributive-Network/PythonMonkey-Crypto-JS-Fullstack-Example
105+
106+
# Troubleshooting Tips
107+
108+
## REPL - pmjs
109+
A basic JavaScript shell, `pmjs`, ships with PythonMonkey.
110+
111+
## CommonJS (require)
112+
If you are having trouble with the CommonJS require function, set environment variable DEBUG='ctx-module*' and you can see the filenames it tries to laod.
113+
114+
### Extra Symbols
115+
Loading the CommonJS subsystem declares some extra symbols which may be helpful in debugging -
116+
- `python.print` - the Python print function
117+
- `python.getenv` - the Python getenv function
118+

build_script.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#! /bin/bash
2+
#
3+
# @file build.sh
4+
# @author Giovanni Tedesco <[email protected]>
5+
# @date Aug 2022
6+
7+
cd `dirname "$0"`
8+
topDir=`pwd`
9+
110
# Get number of CPU cores
211
CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1)
312

@@ -14,3 +23,8 @@ else
1423
cmake ..
1524
fi
1625
cmake --build . -j$CPUS --config Release
26+
27+
cd ../python/pythonmonkey
28+
# npm is used to load JS components, see package.json
29+
cd "${topDir}/python/pythonmonkey/"
30+
npm i

examples/use-python-module.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# @file use-require.py
2+
# Sample code which demonstrates how to use require
3+
# @author Wes Garland, [email protected]
4+
# @date Jun 2023
5+
6+
import pythonmonkey as pm
7+
8+
require = pm.createRequire(__file__)
9+
require('./use-python-module');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { helloWorld } = require('./my-python-module');
2+
helloWorld()
3+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def helloWorld():
2+
print('hello, world!')
3+
4+
exports['helloWorld'] = helloWorld
5+

examples/use-require.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @file use-require.py
2+
# Sample code which demonstrates how to use require
3+
# @author Wes Garland, [email protected]
4+
# @date Jun 2023
5+
6+
import pythonmonkey as pm
7+
8+
require = pm.createRequire(__file__)
9+
require('./use-require/test1');
10+
print("Done")
11+

examples/use-require/test1.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
const makeOutput = require('./test2').makeOutput;
4+
5+
makeOutput('hello world');

examples/use-require/test2.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
exports.makeOutput = function makeOutput()
4+
{
5+
const argv = Array.from(arguments);
6+
argv.unshift('TEST OUTPUT: ');
7+
python.print.apply(null, argv);
8+
}
9+

0 commit comments

Comments
 (0)