Skip to content

Commit

Permalink
feat: support Deno v1.25 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjosepo authored Aug 29, 2022
1 parent 16f6f2f commit 0626ad6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
52 changes: 31 additions & 21 deletions src/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ export class Callback {
result: "pointer",
},
(
_self: bigint,
args: bigint,
kwargs: bigint,
_self: Deno.PointerValue,
args: Deno.PointerValue,
kwargs: Deno.PointerValue,
) => {
return PyObject.from(callback(
kwargs === 0n ? {} : Object.fromEntries(
kwargs === 0 ? {} : Object.fromEntries(
new PyObject(kwargs).asDict()
.entries(),
),
...(args === 0n ? [] : new PyObject(args).valueOf()),
...(args === 0 ? [] : new PyObject(args).valueOf()),
)).handle;
},
);
Expand Down Expand Up @@ -187,13 +187,13 @@ export class Callback {
* C PyObject.
*/
export class PyObject {
constructor(public handle: bigint) {}
constructor(public handle: Deno.PointerValue) {}

/**
* Check if the object is NULL (pointer) or None type in Python.
*/
get isNone() {
return this.handle === 0n ||
return this.handle === 0 ||
this.handle === python.None[ProxiedPyObject].handle;
}

Expand Down Expand Up @@ -289,7 +289,7 @@ export class PyObject {
this.handle,
parseInt(name),
);
if (item !== 0n) {
if (item !== 0) {
return new PyObject(item).proxy;
}
}
Expand All @@ -301,7 +301,7 @@ export class PyObject {
this.handle,
slice.handle,
);
if (item !== 0n) {
if (item !== 0) {
return new PyObject(item).proxy;
}
}
Expand All @@ -318,7 +318,7 @@ export class PyObject {
this.handle,
cstr(name),
);
if (value !== 0n) {
if (value !== 0) {
return new PyObject(value).proxy;
}
}
Expand Down Expand Up @@ -440,10 +440,18 @@ export class PyObject {
const nameBuf = new TextEncoder().encode(
"JSCallback:" + (v.callback.name || "anonymous") + "\0",
);
view.setBigUint64(0, Deno.UnsafePointer.of(nameBuf), LE);
view.setBigUint64(8, v.unsafe.pointer, LE);
view.setBigUint64(
0,
BigInt(Deno.UnsafePointer.of(nameBuf)),
LE,
);
view.setBigUint64(8, BigInt(v.unsafe.pointer), LE);
view.setInt32(16, 0x1 | 0x2, LE);
view.setBigUint64(20, Deno.UnsafePointer.of(nameBuf), LE);
view.setBigUint64(
20,
BigInt(Deno.UnsafePointer.of(nameBuf)),
LE,
);
const fn = py.PyCFunction_NewEx(
struct,
PyObject.from(null).handle,
Expand Down Expand Up @@ -517,7 +525,7 @@ export class PyObject {
const obj = new PyObject(
py.PyObject_GetAttrString(this.handle, cstr(name)),
);
if (obj.handle === 0n) {
if (obj.handle === 0) {
py.PyErr_Clear();
return undefined;
} else {
Expand Down Expand Up @@ -583,10 +591,10 @@ export class PyObject {
*/
asString() {
const str = py.PyUnicode_AsUTF8(this.handle);
if (str === 0n) {
if (str === 0) {
return null;
} else {
return new Deno.UnsafePointerView(str).getCString();
return Deno.UnsafePointerView.getCString(str as bigint);
}
}

Expand Down Expand Up @@ -626,7 +634,7 @@ export class PyObject {
*[Symbol.iterator]() {
const iter = py.PyObject_GetIter(this.handle);
let item = py.PyIter_Next(iter);
while (item !== 0n) {
while (item !== 0) {
yield new PyObject(item);
item = py.PyIter_Next(iter);
}
Expand Down Expand Up @@ -768,7 +776,7 @@ export class PythonError extends Error {
*/
export function maybeThrowError() {
const error = py.PyErr_Occurred();
if (error === 0n) {
if (error === 0) {
return;
}

Expand Down Expand Up @@ -871,7 +879,7 @@ export class Python {
)
.handle,
);
if (module === 0n) {
if (module === 0) {
throw new PythonError("Failed to run module");
}
return new PyObject(module)?.proxy;
Expand All @@ -882,7 +890,7 @@ export class Python {
*/
importObject(name: string) {
const mod = py.PyImport_ImportModule(cstr(name));
if (mod === 0n) {
if (mod === 0) {
maybeThrowError();
throw new PythonError(`Failed to import module ${name}`);
}
Expand Down Expand Up @@ -938,7 +946,9 @@ function toSlice(sliceList: string): PyObject {
.map((pyObject) => pyObject.handle);

const pyTuple_Pack = new Deno.UnsafeFnPointer(
py.PyTuple_Pack,
// TODO: this isn't really a `bigint`, but Deno's type definitions
// haven't been updated to support `number` yet
py.PyTuple_Pack as bigint,
{
parameters: ["i32", ...pySlicesHandle.map(() => "pointer" as const)],
result: "pointer",
Expand Down
22 changes: 11 additions & 11 deletions src/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const SYMBOLS = {
},

PyImport_ImportModule: {
parameters: ["pointer"],
parameters: ["buffer"],
result: "pointer",
},

Expand All @@ -35,7 +35,7 @@ export const SYMBOLS = {
},

PyRun_SimpleString: {
parameters: ["pointer"],
parameters: ["buffer"],
result: "i32",
},

Expand All @@ -50,7 +50,7 @@ export const SYMBOLS = {
},

PyErr_Fetch: {
parameters: ["pointer", "pointer", "pointer"],
parameters: ["buffer", "buffer", "buffer"],
result: "void",
},

Expand All @@ -60,7 +60,7 @@ export const SYMBOLS = {
},

PyDict_SetItemString: {
parameters: ["pointer", "pointer", "pointer"],
parameters: ["pointer", "buffer", "pointer"],
result: "i32",
},

Expand Down Expand Up @@ -90,17 +90,17 @@ export const SYMBOLS = {
},

PyObject_GetAttrString: {
parameters: ["pointer", "pointer"],
parameters: ["pointer", "buffer"],
result: "pointer",
},

PyObject_SetAttrString: {
parameters: ["pointer", "pointer", "pointer"],
parameters: ["pointer", "buffer", "pointer"],
result: "i32",
},

PyObject_HasAttrString: {
parameters: ["pointer", "pointer"],
parameters: ["pointer", "buffer"],
result: "i32",
},

Expand Down Expand Up @@ -200,7 +200,7 @@ export const SYMBOLS = {
},

PyUnicode_DecodeUTF8: {
parameters: ["pointer", "i32", "pointer"],
parameters: ["buffer", "i32", "pointer"],
result: "pointer",
},

Expand Down Expand Up @@ -345,7 +345,7 @@ export const SYMBOLS = {
},

PyImport_ExecCodeModule: {
parameters: ["pointer", "pointer"],
parameters: ["buffer", "pointer"],
result: "pointer",
},

Expand All @@ -355,7 +355,7 @@ export const SYMBOLS = {
},

PyDict_GetItemString: {
parameters: ["pointer", "pointer"],
parameters: ["pointer", "buffer"],
result: "pointer",
},

Expand All @@ -374,7 +374,7 @@ export const SYMBOLS = {
},

PyCFunction_NewEx: {
parameters: ["pointer", "pointer", "pointer"],
parameters: ["buffer", "pointer", "pointer"],
result: "pointer",
},
} as const;
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const decoder = new TextDecoder();

const libDlDef = {
dlopen: {
parameters: ["pointer", "i32"],
parameters: ["buffer", "i32"],
result: "pointer",
},
} as const;
Expand All @@ -22,7 +22,7 @@ export function postSetup(lib: string) {
gnu_get_libc_version: { parameters: [], result: "pointer" },
});
const ptrView = new Deno.UnsafePointerView(
libc.symbols.gnu_get_libc_version(),
BigInt(libc.symbols.gnu_get_libc_version()),
);
const glibcVersion = parseFloat(ptrView.getCString());

Expand All @@ -34,7 +34,7 @@ export function postSetup(lib: string) {
} else if (Deno.build.os === "darwin") {
libdl = Deno.dlopen(`libc.dylib`, {
dlopen: {
parameters: ["pointer", "i32"],
parameters: ["buffer", "i32"],
result: "pointer",
},
});
Expand Down

0 comments on commit 0626ad6

Please sign in to comment.