-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b887916
commit 3a5246b
Showing
23 changed files
with
358 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
/*.bat | ||
deno.lock | ||
plug/ | ||
bun.lockb | ||
node_modules/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preload = ["./plugin.ts"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { add } from "./test.py"; | ||
import { print } from "python:builtins"; | ||
import * as np from "python:numpy"; | ||
|
||
console.log(add(1, 2)); | ||
print("Hello, world!"); | ||
console.log(np.array([1, 2, 3])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def add(a, b): | ||
return a + b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import py, { Python } from "./mod.ts"; | ||
import { Pip, pip } from "./ext/pip.ts"; | ||
|
||
declare global { | ||
const py: Python; | ||
const pip: Pip; | ||
} | ||
|
||
Object.defineProperty(globalThis, "py", { | ||
value: py, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
}); | ||
|
||
Object.defineProperty(globalThis, "pip", { | ||
value: pip, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
}); | ||
|
||
export * from "./mod.ts"; | ||
export * from "./ext/pip.ts"; | ||
export default py; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module "python:*"; | ||
declare module "*.py"; | ||
|
||
import "./src/bun_compat.js"; | ||
export * from "./mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "bunpy", | ||
"version": "0.3.3", | ||
"description": "JavaScript -> Python Bridge for Deno and Bun", | ||
"main": "mod.bun.ts", | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "deno task test && bun test" | ||
}, | ||
"files": [ | ||
"mod.ts", | ||
"ext/pip.ts", | ||
"src/bun_compat.js", | ||
"src/ffi.ts", | ||
"src/python.ts", | ||
"src/symbols.ts", | ||
"src/util.ts", | ||
"plugin.ts" | ||
], | ||
"keywords": [], | ||
"author": "DjDeveloperr", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// TODO: Maybe add support for pip: namespace that automatically installs the module if it's not found. | ||
|
||
// deno-lint-ignore-file no-explicit-any | ||
import { plugin } from "bun"; | ||
import { python } from "./mod.ts"; | ||
|
||
const { dir } = python.builtins; | ||
const { SourceFileLoader } = python.import("importlib.machinery"); | ||
|
||
export function exportModule(mod: any) { | ||
const props = dir(mod).valueOf(); | ||
const exports: Record<string, any> = {}; | ||
for (let prop of props) { | ||
prop = prop.toString(); | ||
exports[prop] = mod[prop]; | ||
} | ||
return exports; | ||
} | ||
|
||
plugin({ | ||
name: "Python Loader", | ||
setup: (build) => { | ||
build.onLoad({ filter: /\.py$/ }, (args) => { | ||
const name = args.path.split("/").pop()!.split(".py")[0]; | ||
const exports = SourceFileLoader(name, args.path).load_module(); | ||
return { | ||
exports: exportModule(exports), | ||
loader: "object", | ||
}; | ||
}); | ||
|
||
build.onResolve({ filter: /.+/, namespace: "python" }, (args) => { | ||
return { path: args.path, namespace: "python" }; | ||
}); | ||
|
||
build.onLoad({ filter: /.+/, namespace: "python" }, (args) => { | ||
const exports = python.import(args.path); | ||
return { | ||
exports: exportModule(exports), | ||
loader: "object", | ||
}; | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { type } from "node:os"; | ||
|
||
if (!("Deno" in globalThis) && "Bun" in globalThis) { | ||
const { dlopen, FFIType, CString, JSCallback, ptr } = await import("bun:ffi"); | ||
class Deno { | ||
static env = { | ||
get(name) { | ||
return Bun.env[name]; | ||
}, | ||
}; | ||
|
||
static build = { | ||
os: type().toLowerCase(), | ||
}; | ||
|
||
static transformFFIType(type) { | ||
switch (type) { | ||
case "void": | ||
return FFIType.void; | ||
case "i32": | ||
return FFIType.i64_fast; | ||
case "i64": | ||
return FFIType.i64; | ||
case "f32": | ||
return FFIType.f32; | ||
case "f64": | ||
return FFIType.f64; | ||
case "pointer": | ||
case "buffer": | ||
return FFIType.ptr; | ||
case "u32": | ||
return FFIType.u64_fast; | ||
default: | ||
throw new Error("Type not supported: " + type); | ||
} | ||
} | ||
|
||
static dlopen(path, symbols) { | ||
const bunSymbols = {}; | ||
for (const name in symbols) { | ||
const symbol = symbols[name]; | ||
if ("type" in symbol) { | ||
throw new Error("Symbol type not supported"); | ||
} else { | ||
bunSymbols[name] = { | ||
args: symbol.parameters.map((type) => this.transformFFIType(type)), | ||
returns: this.transformFFIType(symbol.result), | ||
}; | ||
} | ||
} | ||
const lib = dlopen(path, bunSymbols); | ||
return lib; | ||
} | ||
|
||
static UnsafeCallback = class UnsafeCallback { | ||
constructor(def, fn) { | ||
this.inner = new JSCallback(fn, { | ||
args: def.parameters.map((type) => Deno.transformFFIType(type)), | ||
returns: Deno.transformFFIType(def.result), | ||
}); | ||
this.pointer = this.inner.ptr; | ||
} | ||
|
||
close() { | ||
this.inner.close(); | ||
} | ||
}; | ||
|
||
static UnsafePointerView = class UnsafePointerView { | ||
static getCString(ptr) { | ||
return new CString(ptr); | ||
} | ||
|
||
constructor(ptr) { | ||
this.ptr = ptr; | ||
} | ||
|
||
getCString() { | ||
return new CString(this.ptr); | ||
} | ||
}; | ||
|
||
static UnsafePointer = class UnsafePointer { | ||
static equals(a, b) { | ||
return a === b; | ||
} | ||
|
||
static create(a) { | ||
return Number(a); | ||
} | ||
|
||
static of(buf) { | ||
return ptr(buf); | ||
} | ||
|
||
static value(ptr) { | ||
return ptr; | ||
} | ||
}; | ||
|
||
static test(name, fn) { | ||
globalThis.DenoTestCompat(name, fn); | ||
} | ||
} | ||
|
||
globalThis.Deno = Deno; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.