Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

BF-31 #8

Merged
merged 59 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
abf66f7
implement python to Javascript string conversion
zollqir Mar 14, 2023
a18a974
fix missing packages in setup script
zollqir Mar 14, 2023
9d80465
add tests for string function arguments
zollqir Mar 14, 2023
fd0b5aa
chore: multithreaded compilation
Xmader Mar 15, 2023
b089340
ci: multithreaded compilation in ci
Xmader Mar 15, 2023
5701b44
chore: remove unused `explore` module
Xmader Mar 16, 2023
b2717c8
chore: add `Testing/Temporary` to `.gitignore`
Xmader Mar 16, 2023
b0dd745
feat(bigint): initial implementation of BigInt
Xmader Mar 18, 2023
facb4aa
refactor(bigint): use public SpiderMonkey API to read the sign bit
Xmader Mar 18, 2023
2ed0c80
fix(bigint): properly get the digit storage if the number cannot fit …
Xmader Mar 18, 2023
3fcc3d0
feat(bigint): signed BigInt
Xmader Mar 18, 2023
0f04c52
perf(bigint): skip setting the sign bit if the number is positive
Xmader Mar 18, 2023
1188540
feat(bigint): handle boxed `BigInt` as in `Object(1n)`
Xmader Mar 18, 2023
8eb93cc
test(bigint): write tests for BigInt
Xmader Mar 19, 2023
0db2a4f
test(bigint): write tests for boxed BigInt
Xmader Mar 19, 2023
e32f33c
feat(bigint): check if integer exceeds Number.MAX_SAFE_INTEGER
Xmader Mar 20, 2023
f54a0d4
refactor(pm.null): use designated initializers (C++20 feature)
Xmader Mar 20, 2023
421133e
feat(bigint): create a custom class on PythonMonkey that maps to JS B…
Xmader Mar 20, 2023
f3b2ee3
fix(pm.null): `pythonmonkey.null` shouldn't be instantiated
Xmader Mar 20, 2023
3074fc4
revert(pm.null): `Py_TPFLAGS_DISALLOW_INSTANTIATION` is new from Pyth…
Xmader Mar 20, 2023
bccfeff
feat(bigint): convert Js BigInt to pm.bigint
Xmader Mar 21, 2023
032bfa8
fix(callJSFunc): fail-fast on exception
Xmader Mar 21, 2023
71cf248
fix(bigint): long is 32-bit on Win64 or 32bit *nix
Xmader Mar 21, 2023
68e172f
fix(bigint): print out the `IntType` as a string
Xmader Mar 21, 2023
9954fc2
feat(bigint): convert Python int/pm.bigint to JS BigInt
Xmader Mar 21, 2023
924c622
fix(bigint): use the JScontext from `callJSFunc`
Xmader Mar 21, 2023
4c68afa
implement function coercion
zollqir Mar 21, 2023
e378867
Merge branch 'BF-31' into BF-36
Xmader Mar 21, 2023
6c61320
fix(callJSFunc): fail-fast on exception
Xmader Mar 21, 2023
08d02eb
fix(bigint): segfault (core dumped) when converting from hex string
Xmader Mar 22, 2023
275ffd0
add tests for javascript functions taking python functions as parameters
zollqir Mar 22, 2023
72c9eb4
fix(bigint): create a new object instead of reusing the object for int 0
Xmader Mar 22, 2023
fe7db7f
perf(bigint): use `mozilla::Span` constructor directly
Xmader Mar 22, 2023
cf1fe80
feat(bigint): convert negative pm.bigint to JS BigInt
Xmader Mar 22, 2023
21702de
Merge branch 'BF-31' into BF-36
Xmader Mar 22, 2023
185e363
test(bigint): write tests for bigints as function arguments
Xmader Mar 23, 2023
6604046
fix(bigint): OverflowError instead of TypeError should be raised on i…
Xmader Mar 23, 2023
b5069a1
fix(bigint): ints larger than 64bits should also ask user to use pm.b…
Xmader Mar 23, 2023
e22979a
test(bigint): write tests for adding bigints in JS function
Xmader Mar 23, 2023
1fc5167
test(bigint): test with real-world example of calculating the factorial
Xmader Mar 23, 2023
e6453bf
test(bigint): test with real-world use case of calculating the crc32 …
Xmader Mar 23, 2023
53db0a1
fix(bigint): include pm.bigint in jsTypeFactory TypeError message
Xmader Mar 23, 2023
8e0e46e
feat(bigint): fast path for ints that can fit in one uint64
Xmader Mar 24, 2023
8a92587
fix(bigint): pm.bigint object malformed on NoMemory error
Xmader Mar 24, 2023
da3ebec
Merge pull request #7 from Distributive-Network/BF-33
zollqir Mar 27, 2023
6403833
Merge branch 'main' into BF-31
zollqir Mar 27, 2023
8dcbcf5
remove auto, add names to TODOs
zollqir Mar 27, 2023
be0fdc7
Merge pull request #9 from Distributive-Network/BF-36
zollqir Mar 27, 2023
b98f863
fix(string): UCS4-range strings do not work properly to JS functions
Xmader Mar 28, 2023
17925fa
perf(string): remove a `memcpy` call when handling UCS4-range strings…
Xmader Mar 28, 2023
bf565b5
fix(int): properly handle negative integers
Xmader Mar 28, 2023
aee3d05
implement variadic functions, support coercion of PyCFunctions
zollqir Mar 29, 2023
e81eb5c
add sudo inside setup.sh, update README
zollqir Mar 29, 2023
3c6ff10
Merge branch 'main' into BF-31
Xmader Mar 31, 2023
dcd5a6b
Update src/jsTypeFactory.cc
zollqir Apr 12, 2023
a18c0e2
Update src/jsTypeFactory.cc
zollqir Apr 12, 2023
3fa4629
refactor PyFunction and PyCFunction coercing to remove duplicated code
zollqir Apr 12, 2023
2e0b0c3
Merge pull request #10 from Distributive-Network/BF-42
zollqir Apr 12, 2023
e4f9ee8
refactor(pyTypeFactory): rename variable names to reflect that the so…
Xmader Apr 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
- name: Build-Library
if: ${{ steps.cache-spidermonkey.outputs.cache-hit != 'true' }}
run: |
# Get number of CPU cores
CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1)
echo "Building SpiderMonkey"
#install rust compiler
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh -s -- -y
Expand All @@ -48,14 +50,14 @@ jobs:
mkdir _build
cd _build
../configure --disable-jemalloc --with-system-zlib --with-intl-api --enable-optimize
make
make -j$CPUS
sudo make install
cd ../../../..
echo "Building the library"
mkdir build
cd build
cmake ..
cmake --build .
cmake --build . -j$CPUS
echo "Build complete"
- name: google-tests
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ firefox-*/*
firefox-*/
tests/__pycache__/*
tests/python/__pycache__/*
Testing/Temporary
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ this package to execute our complex `dcp-client` library, which is written in JS
- [done] JS exceptions propagate to Python
- [done] Implement `eval()` function in Python which accepts JS code and returns JS->Python coerced values
- [underway] NodeJS+NPM-compatible CommonJS module system
- [underway] Python strings coerce to JS strings
- [done] Python strings coerce to JS strings
- [done] Python intrinsics coerce to JS intrinsics
- Python dicts coerce to JS objects
- Python `require` function, returns a coerced dict of module exports
- Python functions coerce to JS function wrappers
- [done] Python functions coerce to JS function wrappers
- CommonJS module system .py loader, loads Python modules for use by JS
- JS object->Python dict coercion supports inherited-property lookup (via __getattribute__?)
- Python host environment supplies event loop, including EventEmitter, setTimeout, etc.
Expand Down Expand Up @@ -64,7 +64,7 @@ this package to execute our complex `dcp-client` library, which is written in JS
2. In the build folder `cd` into the `tests` directory and run `ctest`.
```bash
# From the root directory we do the following (after compiling the project)
$ cd buid/tests
$ cd build/tests
$ ctest
```
Alternatively, from the root directory, run ``./test_script.sh``
Expand Down
5 changes: 4 additions & 1 deletion build_script.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Get number of CPU cores
CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1)

DIR="build"
if [ ! -d "$DIR" ]; then
### Take action if $DIR exists ###
Expand All @@ -6,4 +9,4 @@ fi

cd build
cmake ..
cmake --build .
cmake --build . -j$CPUS
29 changes: 23 additions & 6 deletions include/IntType.hh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file IntType.hh
* @author Caleb Aikens (caleb@distributive.network) & Giovanni Tedesco (giovanni@distributive.network)
* @author Caleb Aikens (caleb@distributive.network) & Giovanni Tedesco (giovanni@distributive.network) & Tom Tang (xmader@distributive.network)
* @brief Struct for representing python ints
* @version 0.1
* @date 2022-07-27
* @version 0.2
* @date 2023-03-16
*
* @copyright Copyright (c) 2022
* @copyright Copyright (c) 2023
*
*/

Expand All @@ -15,19 +15,36 @@
#include "PyType.hh"
#include "TypeEnum.hh"

#include <jsapi.h>

#include <Python.h>

#include <iostream>

/**
* @brief This struct represents the 'int' type in Python, which is represented as a 'long' in C++. It inherits from the PyType struct
* @brief This struct represents the 'int' type (arbitrary-precision) in Python. It inherits from the PyType struct
*/
struct IntType : public PyType {
public:
IntType(PyObject *object);
IntType(long n);

/**
* @brief Construct a new IntType object from a JS::BigInt.
*
* @param cx - javascript context pointer
* @param str - JS::BigInt pointer
*/
IntType(JSContext *cx, JS::BigInt *bigint);

const TYPE returnType = TYPE::INT;
long getValue() const;

/**
* @brief Convert the IntType object to a JS::BigInt
*
* @param cx - javascript context pointer
*/
JS::BigInt *toJsBigInt(JSContext *cx);

protected:
virtual void print(std::ostream &os) const override;
Expand Down
27 changes: 25 additions & 2 deletions include/jsTypeFactory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@

#include <jsapi.h>


struct PythonExternalString;

/**
* @brief Function that makes a UTF16-encoded copy of a UCS4 string
*
* @param chars - pointer to the UCS4-encoded string
* @param length - length of chars in code points
* @param outStr - UTF16-encoded out-parameter string
* @return size_t - length of outStr (counting surrogate pairs as 2)
*/
size_t UCS4ToUTF16(const uint32_t *chars, size_t length, uint16_t *outStr);
/**
* @brief Function that takes a PyType and returns a corresponding JS::Value, doing shared memory management when necessary
* @brief Function that takes a PyObject and returns a corresponding JS::Value, doing shared memory management when necessary
*
* @param cx - Pointer to the JSContext
* @param object - Pointer to the PyObject who's type and value we wish to encapsulate
* @return JS::Value - A JS::Value corresponding to the PyType
*/
JS::Value jsTypeFactory(PyObject *object);
JS::Value jsTypeFactory(JSContext *cx, PyObject *object);

/**
* @brief Helper function for jsTypeFactory to create a JSFunction* through JS_NewFunction that knows how to call a python function.
*
* @param cx - Pointer to the JSContext
* @param argc - The number of arguments the JSFunction expects
* @param vp - The return value of the JSFunction
* @return true - Function executed successfully
* @return false - Function did not execute successfully and an exception has been set
*/
bool callPyFunc(JSContext *cx, unsigned int argc, JS::Value *vp);
#endif
40 changes: 0 additions & 40 deletions include/modules/explore/explore.hh

This file was deleted.

4 changes: 3 additions & 1 deletion include/modules/pythonmonkey/pythonmonkey.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

#include <Python.h>

#define PythonMonkey_Null PyObject_GetAttrString(PyState_FindModule(&pythonmonkey), "null") /**< macro for python null object*/
#define PythonMonkey_Null PyObject_GetAttrString(PyState_FindModule(&pythonmonkey), "null") /**< macro for pythonmonkey.null object*/
#define PythonMonkey_BigInt PyObject_GetAttrString(PyState_FindModule(&pythonmonkey), "bigint") /**< macro for pythonmonkey.bigint class object */

static JSContext *GLOBAL_CX; /**< pointer to PythonMonkey's JSContext */
static JS::Rooted<JSObject *> *global; /**< pointer to the global object of PythonMonkey's JSContext */
static JSAutoRealm *autoRealm; /**< pointer to PythonMonkey's AutoRealm */

/**
* @brief Destroys the JSContext and deletes associated memory. Called when python quits or faces a fatal exception.
Expand Down
4 changes: 2 additions & 2 deletions include/pyTypeFactory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ PyType *pyTypeFactory(PyObject *object);
* @brief Function that takes a JS::Value and returns a corresponding PyType* object, doing shared memory management when necessary
*
* @param cx - Pointer to the javascript context of the JS::Value
* @param global - Pointer to the javascript global object
* @param thisObj - Pointer to the JS `this` object for the value's scope
* @param rval - Pointer to the JS::Value who's type and value we wish to encapsulate
* @return PyType* - Pointer to a PyType object corresponding to the JS::Value
*/
PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *global, JS::Rooted<JS::Value> *rval);
PyType *pyTypeFactory(JSContext *cx, JS::Rooted<JSObject *> *thisObj, JS::Rooted<JS::Value> *rval);

/**
* @brief Helper function for pyTypeFactory to create FuncTypes through PyCFunction_New
Expand Down
15 changes: 9 additions & 6 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
apt-get update --yes
apt-get upgrade --yes
apt-get install cmake python3-dev python3-pytest doxygen graphviz gcovr llvm m4 --yes
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh -s -- -y #install rust compiler
# Get number of CPU cores
CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1)

sudo apt-get update --yes
sudo apt-get upgrade --yes
sudo apt-get install cmake python3-dev python3-pytest doxygen graphviz gcovr llvm g++ pkg-config m4 --yes
sudo curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh -s -- -y #install rust compiler
wget -q https://ftp.mozilla.org/pub/firefox/releases/102.2.0esr/source/firefox-102.2.0esr.source.tar.xz
tar xf firefox-102.2.0esr.source.tar.xz
cd firefox-102.2.0/js
Expand All @@ -13,5 +16,5 @@ chmod +x ./configure
mkdir _build
cd _build
../configure --disable-jemalloc --with-system-zlib --with-intl-api --enable-optimize
make
make install
make -j$CPUS
sudo make install
23 changes: 1 addition & 22 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
include_directories(${CMAKE_CURRENT_LIST_DIR})

#list(APPEND EXPLORE_SOURCE_FILES ${SOURCE_FILES} "${CMAKE_SOURCE_DIR}/src/modules/explore/explore.cc")
list(APPEND PYTHONMONKEY_SOURCE_FILES ${SOURCE_FILES} "${CMAKE_SOURCE_DIR}/src/modules/pythonmonkey/pythonmonkey.cc")

# add_library(explore SHARED
# ${EXPLORE_SOURCE_FILES}
# )

add_library(pythonmonkey SHARED
${PYTHONMONKEY_SOURCE_FILES}
)
Expand All @@ -16,18 +11,9 @@ execute_process(
OUTPUT_VARIABLE pyloc
)

# target_include_directories(explore PUBLIC ..)
target_include_directories(pythonmonkey PUBLIC ..)

if(WIN32)
# set_target_properties(
# explore
# PROPERTIES
# PREFIX ""
# SUFFIX ".pyd"
# OUTPUT_NAME "explore"
# CXX_STANDARD 17
# )
set_target_properties(
pythonmonkey
PROPERTIES
Expand All @@ -37,23 +23,16 @@ if(WIN32)
CXX_STANDARD 17
)
elseif(UNIX)
# set_target_properties(
# explore
# PROPERTIES
# PREFIX ""
# SUFFIX ".so"
# )
set_target_properties(
pythonmonkey
PROPERTIES
PREFIX ""
SUFFIX ".so"
)
endif()
# target_link_libraries(explore ${PYTHON_LIBRARIES})

target_link_libraries(pythonmonkey ${PYTHON_LIBRARIES})
target_link_libraries(pythonmonkey ${SPIDERMONKEY_LIBRARIES})

# target_include_directories(explore PRIVATE ${PYTHON_INCLUDE_DIR})
target_include_directories(pythonmonkey PRIVATE ${PYTHON_INCLUDE_DIR})
target_include_directories(pythonmonkey PRIVATE ${SPIDERMONKEY_INCLUDE_DIR})
Loading