Skip to content

Commit

Permalink
use 'Deno.PointerValue
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac committed Jul 17, 2023
1 parent ac851fa commit 7cbb547
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
21 changes: 10 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
export * from "./types.ts";

export function cstr2ptr(cstr: string): Deno.UnsafePointer {
export function cstr2ptr(cstr: string): Deno.PointerValue {
const buffer = new Uint8Array([...new TextEncoder().encode(cstr), 0]);
return Deno.UnsafePointer.of(buffer);
}

export function ptr2cstr(ptr: Deno.UnsafePointer): string {
if (ptr.value === 0n) {
return "";
}
export function ptr2cstr(ptr: Deno.PointerValue): string {
if (ptr === null) return "";
if (Deno.UnsafePointer.equals(ptr, null)) return "";
return new Deno.UnsafePointerView(ptr).getCString();
}

export function ptr2cstr_unchecked(ptr: Deno.UnsafePointer): string {
return new Deno.UnsafePointerView(ptr).getCString();
export function ptr2cstr_unchecked(ptr: Deno.PointerValue): string {
return new Deno.UnsafePointerView(ptr!).getCString();
}

/**
* Returns a boolean indicating if an environment is little endian.
*/
export function isLittleEndian(): boolean {
/*
* Set the uint16 view to a value having distinguishable lower and higher order words.
*
* 4660 => 0x1234 => 0x12 0x34 => '00010010 00110100' => (0x12,0x34) == (18,52)
*/
* Set the uint16 view to a value having distinguishable lower and higher order words.
*
* 4660 => 0x1234 => 0x12 0x34 => '00010010 00110100' => (0x12,0x34) == (18,52)
*/
const uint16 = Uint16Array.of(0x1234);

// Create a uint8 view on top of the uint16 buffer:
Expand Down
12 changes: 6 additions & 6 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type ParseName<T> = T extends `unsigned ${infer R} ${infer F}(${infer Rest})`
}
>
: T extends `${infer R} ${infer F}(${infer Rest})` ? Record<
Trim<F>,
{ parameters: ParseParams<Trim<Rest>>; result: ToNativeType<R> }
>
Trim<F>,
{ parameters: ParseParams<Trim<Rest>>; result: ToNativeType<R> }
>
: never;

type ParseParams<T> = T extends "" ? []
Expand Down Expand Up @@ -83,9 +83,9 @@ type ParseReturn<T> = T extends `unsigned ${infer R} ${infer F}(${infer Rest})`
(...args: ParseFnParams<Trim<Rest>>) => ToJsType<`unsigned ${R}`>
>
: T extends `${infer R} ${infer F}(${infer Rest})` ? Record<
Trim<F>,
(...args: ParseFnParams<Trim<Rest>>) => ToJsType<R>
>
Trim<F>,
(...args: ParseFnParams<Trim<Rest>>) => ToJsType<R>
>
: never;

type ParseFnParams<T> = T extends "" ? []
Expand Down
2 changes: 1 addition & 1 deletion types_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from "https://deno.land/std@0.103.0/testing/asserts.ts";
import { assert } from "https://deno.land/std@0.194.0/testing/asserts.ts";
import * as ffi from "./mod.ts";

function assertType<
Expand Down

0 comments on commit 7cbb547

Please sign in to comment.