From 0626ad6bbddb6b27071bdd463796bce4d6a5bfa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20Jos=C3=A9povic?= <44372776+tjosepo@users.noreply.github.com> Date: Mon, 29 Aug 2022 08:09:00 -0400 Subject: [PATCH] feat: support Deno v1.25 (#25) --- src/python.ts | 52 ++++++++++++++++++++++++++++++-------------------- src/symbols.ts | 22 ++++++++++----------- src/util.ts | 6 +++--- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/python.ts b/src/python.ts index 11dccda..5431681 100644 --- a/src/python.ts +++ b/src/python.ts @@ -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; }, ); @@ -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; } @@ -289,7 +289,7 @@ export class PyObject { this.handle, parseInt(name), ); - if (item !== 0n) { + if (item !== 0) { return new PyObject(item).proxy; } } @@ -301,7 +301,7 @@ export class PyObject { this.handle, slice.handle, ); - if (item !== 0n) { + if (item !== 0) { return new PyObject(item).proxy; } } @@ -318,7 +318,7 @@ export class PyObject { this.handle, cstr(name), ); - if (value !== 0n) { + if (value !== 0) { return new PyObject(value).proxy; } } @@ -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, @@ -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 { @@ -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); } } @@ -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); } @@ -768,7 +776,7 @@ export class PythonError extends Error { */ export function maybeThrowError() { const error = py.PyErr_Occurred(); - if (error === 0n) { + if (error === 0) { return; } @@ -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; @@ -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}`); } @@ -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", diff --git a/src/symbols.ts b/src/symbols.ts index 856d63f..ebd78cc 100644 --- a/src/symbols.ts +++ b/src/symbols.ts @@ -25,7 +25,7 @@ export const SYMBOLS = { }, PyImport_ImportModule: { - parameters: ["pointer"], + parameters: ["buffer"], result: "pointer", }, @@ -35,7 +35,7 @@ export const SYMBOLS = { }, PyRun_SimpleString: { - parameters: ["pointer"], + parameters: ["buffer"], result: "i32", }, @@ -50,7 +50,7 @@ export const SYMBOLS = { }, PyErr_Fetch: { - parameters: ["pointer", "pointer", "pointer"], + parameters: ["buffer", "buffer", "buffer"], result: "void", }, @@ -60,7 +60,7 @@ export const SYMBOLS = { }, PyDict_SetItemString: { - parameters: ["pointer", "pointer", "pointer"], + parameters: ["pointer", "buffer", "pointer"], result: "i32", }, @@ -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", }, @@ -200,7 +200,7 @@ export const SYMBOLS = { }, PyUnicode_DecodeUTF8: { - parameters: ["pointer", "i32", "pointer"], + parameters: ["buffer", "i32", "pointer"], result: "pointer", }, @@ -345,7 +345,7 @@ export const SYMBOLS = { }, PyImport_ExecCodeModule: { - parameters: ["pointer", "pointer"], + parameters: ["buffer", "pointer"], result: "pointer", }, @@ -355,7 +355,7 @@ export const SYMBOLS = { }, PyDict_GetItemString: { - parameters: ["pointer", "pointer"], + parameters: ["pointer", "buffer"], result: "pointer", }, @@ -374,7 +374,7 @@ export const SYMBOLS = { }, PyCFunction_NewEx: { - parameters: ["pointer", "pointer", "pointer"], + parameters: ["buffer", "pointer", "pointer"], result: "pointer", }, } as const; diff --git a/src/util.ts b/src/util.ts index 5c96ecf..9a88a7b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,7 +3,7 @@ export const decoder = new TextDecoder(); const libDlDef = { dlopen: { - parameters: ["pointer", "i32"], + parameters: ["buffer", "i32"], result: "pointer", }, } as const; @@ -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()); @@ -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", }, });