Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fdb9424
Merge branch 'BF-61' into BF-54
Xmader May 10, 2023
0d2f621
feat: prepare to bundle PythonMonkey as a package
Xmader May 17, 2023
a77fe55
feat: re-export public APIs to add type hints & documentations
Xmader May 17, 2023
075c401
refactor: use stub file for public API type hints & documentations
Xmader May 17, 2023
f94e145
feat: register JS/Python implemented browser/Node.js APIs
Xmader May 18, 2023
d5f630a
refactor: JS `internalBinding` function creation
Xmader May 18, 2023
4bbd3fb
fix: `pm._internalBinding` still requires object coercion support
Xmader May 18, 2023
08cc593
refactor: rewrite `setTimeout`/`clearTimeout` in JS using internal bi…
Xmader May 18, 2023
b4103c4
feat: loosely implement `console` API
Xmader May 19, 2023
be83d1e
style: rephrase `Register APIs` -> `Install APIs`
Xmader May 19, 2023
98cd4ec
feat: implement base64 utility functions `atob`/`btoa`, taken impleme…
Xmader May 19, 2023
3f19985
refactor: internal bindings creation, individual modules exporting th…
Xmader May 19, 2023
e2bb428
refactor: internal binding modules define `methodSpecs` under the C++…
Xmader May 19, 2023
f5770ee
refactor: move the `defineGlobal` helper function to `utils` internal…
Xmader May 19, 2023
c8699ad
feat(event-loop): support passing a `code` string to `setTimeout` as …
Xmader May 23, 2023
bbf84e0
feat(console): implement `console` API with pretty print, reusing the…
Xmader May 24, 2023
632b1e2
fix(console): the `util.instpect.custom` symbol is defined in the glo…
Xmader May 24, 2023
5b1c3c5
fix(console): re-export the `nodejs.util.inspect.custom` symbol as th…
Xmader May 24, 2023
be17b41
feat(console): re-export the `Console` constructor as global `console…
Xmader May 24, 2023
a1e7fd0
Merge branch 'BF-40' into BF-54
Xmader May 30, 2023
de45643
Merge branch 'main' into BF-54
Xmader May 30, 2023
b13d9ea
Merge branch 'main' into BF-54
Xmader Jun 23, 2023
7863916
Revert "feat: implement base64 utility functions `atob`/`btoa`, taken…
Xmader Jun 23, 2023
5650ab2
Revert "feat(event-loop): support passing a `code` string to `setTime…
Xmader Jun 23, 2023
3b4478c
Revert "refactor: rewrite `setTimeout`/`clearTimeout` in JS using int…
Xmader Jun 23, 2023
06ac480
remove `timers` InternalBinding
Xmader Jun 23, 2023
0499ac0
fix merging typo
Xmader Jun 23, 2023
0ff5a8a
Merge branch 'main' into Xmader/feat/console-api
Xmader Jun 23, 2023
f44e6a0
Merge branch 'Xmader/feat/console-api' into wes/module-system
Xmader Jun 29, 2023
182be98
refactor(console): migrate to the new module system
Xmader Jun 29, 2023
eb42e8f
refactor(console): move to files to the `builtin_modules` directory
Xmader Jun 29, 2023
4544d1a
add type definitions for `globalThis.python`
Xmader Jun 29, 2023
a1c4d30
add file header to src/internalBinding/utils.cc
Xmader Jun 29, 2023
40e94ae
refactor(console): cleanup `console` module, and move node-like util …
Xmader Jun 29, 2023
a43149b
feat: type resolution of modules from `builtin_modules`
Xmader Jun 29, 2023
d922c01
refactor: move type declarations for `internalBinding` APIs to `built…
Xmader Jun 29, 2023
02cade7
Merge branch 'main' into Xmader/feat/console-api
Xmader Jul 5, 2023
caf8ae9
Merge branch 'main' into Xmader/feat/console-api
Xmader Jul 5, 2023
7de62e0
feat: export internalBinding function as `pm.internalBinding`, and th…
Xmader Jul 5, 2023
f88069c
docs: add top-of-file comment to src/internalBinding.cc
Xmader Jul 5, 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function(pythonmonkey_add_external PYTHONMONKEY_EXTERNAL)
set(PYTHONMONKEY_EXTERNAL_FILES ${PYTHONMONKEY_EXTERNAL_FILES} PARENT_SCOPE)
endfunction()

file (GLOB SOURCE_FILES "src/*.cc") # Find all C++ files in the src directory
file (GLOB SOURCE_FILES "src/*.cc" "src/internalBinding/*.cc") # Find all C++ files in the src directory
file (GLOB HEADER_FILES "include/*.hh") # Find all header files in the include directory
file (GLOB PYTHON_FILES "python/*.cc" "python/*.hh") # Find all the python bindings in the python directory

Expand Down
23 changes: 23 additions & 0 deletions include/internalBinding.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file internalBinding.hh
* @author Tom Tang (xmader@distributive.network)
* @brief
* @version 0.1
* @date 2023-05-16
*
* @copyright Copyright (c) 2023 Distributive Corp.
*
*/

#include <jsapi.h>
#include <Python.h>

namespace InternalBinding {
extern JSFunctionSpec utils[];
}

JSObject *createInternalBindingsForNamespace(JSContext *cx, JSFunctionSpec *methodSpecs);
JSObject *getInternalBindingsByNamespace(JSContext *cx, JSLinearString *namespaceStr);

JSFunction *createInternalBinding(JSContext *cx);
PyObject *getInternalBindingPyFn(JSContext *cx);
1 change: 1 addition & 0 deletions python/pythonmonkey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Export public PythonMonkey APIs
from .pythonmonkey import *
from .require import *

Expand Down
77 changes: 66 additions & 11 deletions python/pythonmonkey/builtin_modules/console.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
/**
* @file console.js
* Temporary implementation of console.log etc.
* @author Wes Garland, wes@distributive.network
* @author Tom Tang <xmader@distributive.network>
* @date June 2023
*/
function Console(print)
{
this.log = print;
this.debug = print;
this.error = print;
this.warn = print;
this.info = print;

const { customInspectSymbol, format } = require("util");

/** @typedef {(str: string) => void} WriteFn */

/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console_API
*/
// TODO (Tom Tang): It's easier to copy implementations from Node.js version 8 than Node.js 20,
// see https://github.com/nodejs/node/blob/v8.17.0/lib/console.js
// TODO (Tom Tang): adhere https://console.spec.whatwg.org/
class Console {
/** @type {WriteFn} */
#writeToStdout;
/** @type {WriteFn} */
#writeToStderr;

/**
* @param {WriteFn} writeToStdout
* @param {WriteFn} writeToStderr
*/
constructor(writeToStdout, writeToStderr) {
this.#writeToStdout = writeToStdout
this.#writeToStderr = writeToStderr
}

/**
* @return {string}
*/
#formatToStr(...args) {
return format(...args) + "\n"
}

log(...args) {
this.#writeToStdout(this.#formatToStr(...args))
}

warn(...args) {
this.#writeToStderr(this.#formatToStr(...args))
}

// TODO (Tom Tang): implement more methods

/**
* Re-export the `Console` constructor as global `console.Console`, like in Node.js
*/
get Console() {
return Console
}

/**
* Export the `nodejs.util.inspect.custom` symbol as a static property of `Console`
*/
static customInspectSymbol = customInspectSymbol;
}

if (!globalThis.console)
globalThis.console = new Console(python.print);
// https://github.com/nodejs/node/blob/v20.1.0/lib/internal/console/constructor.js#L681-L685
Console.prototype.debug = Console.prototype.log;
Console.prototype.info = Console.prototype.log;
Console.prototype.error = Console.prototype.warn;

if (!globalThis.console) {
globalThis.console = new Console(
python.stdout_write /* sys.stdout.write */,
python.stderr_write /* sys.stderr.write */
);
}

exports.Console = Console;
33 changes: 33 additions & 0 deletions python/pythonmonkey/builtin_modules/internal-binding.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file internal-binding.d.ts
* @author Tom Tang <xmader@distributive.network>
* @date June 2023
*/

/**
* Note: `internalBinding` APIs are generally unsafe as they do not perform argument type checking, etc.
* Argument checking should be done in JavaScript side.
*/
declare function internalBinding(namespace: string): any; // catch-all

declare function internalBinding(namespace: "utils"): {
defineGlobal(name: string, value: any): void;

isAnyArrayBuffer(obj: any): obj is (ArrayBuffer | SharedArrayBuffer);
isPromise<T>(obj: any): obj is Promise<T>;
isRegExp(obj: any): obj is RegExp;
isTypedArray(obj: any): obj is TypedArray;

/**
* Get the promise state (fulfilled/rejected/pending) and result (either fulfilled resolution or rejection reason)
*/
getPromiseDetails(promise: Promise<any>): [state: PromiseState.Pending] | [state: PromiseState.Fulfilled | PromiseState.Rejected, result: any];

/**
* Get the proxy target object and handler
* @return `undefined` if it's not a proxy
*/
getProxyDetails<T extends object>(proxy: T): undefined | [target: T, handler: ProxyHandler<T>];
};

export = internalBinding;
10 changes: 10 additions & 0 deletions python/pythonmonkey/builtin_modules/internal-binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
Re-export `internalBinding` to JS
"""

import pythonmonkey as pm

"""
See function declarations in ./internal-binding.d.ts
"""
exports = pm.internalBinding
Loading