Skip to content

Commit d54366b

Browse files
authored
fix: update ffi api usage due to breaking changes in pointer api (#51)
* fix: new Deno update * chore: fmt
1 parent b07deab commit d54366b

File tree

14 files changed

+73
-39
lines changed

14 files changed

+73
-39
lines changed

scripts/build_skia.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const CURRENT_HASH = "2290b0b75a8abb80e23d9cb9aced5b5cebbf702d";
1+
const _CURRENT_HASH = "2290b0b75a8abb80e23d9cb9aced5b5cebbf702d";
22
const CURRENT_HASH_SHORT = "2290b0b";
33

44
Deno.chdir(new URL("../skia", import.meta.url));
@@ -56,6 +56,7 @@ if (Deno.env.get("SKIA_FROM_SOURCE") !== "1") {
5656
}
5757
try {
5858
Deno.mkdirSync("./out/Release", { recursive: true });
59+
// deno-lint-ignore no-empty
5960
} catch (_) {}
6061
for (const name of toDownload) {
6162
let file = name;
@@ -78,6 +79,7 @@ if (Deno.env.get("SKIA_FROM_SOURCE") !== "1") {
7879
Deno.exit(0);
7980
}
8081

82+
// deno-lint-ignore no-explicit-any
8183
const BUILD_ARGS: Record<string, any> = {
8284
cc: Deno.build.os === "windows" ? '"clang-cl"' : '"clang"',
8385
cxx: Deno.build.os === "windows" ? '"clang-cl"' : '"clang++"',

src/canvas.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class Canvas {
8686
width,
8787
height,
8888
);
89-
if (this.#ptr === 0) {
89+
if (this.#ptr === null) {
9090
throw new Error("Failed to create canvas");
9191
}
9292
CANVAS_FINALIZER.register(this, this.#ptr);
@@ -126,17 +126,20 @@ export class Canvas {
126126
OUT_DATA_PTR,
127127
);
128128

129-
if (bufptr === 0) {
129+
if (bufptr === null) {
130130
throw new Error("Failed to encode canvas");
131131
}
132132

133133
const size = OUT_SIZE[0];
134-
const ptr = OUT_DATA[0];
134+
const ptr = Deno.UnsafePointer.create(OUT_DATA[0]);
135135
const buffer = new Uint8Array(getBuffer(bufptr, 0, size));
136136
SK_DATA_FINALIZER.register(buffer, ptr);
137137
return buffer;
138138
}
139139

140+
/**
141+
* Creates a data url from the canvas data
142+
*/
140143
toDataURL(format: ImageFormat = "png", quality = 100) {
141144
const buffer = this.encode(format, quality);
142145
return `data:image/${format};base64,${encodeBase64(buffer)}`;
@@ -168,6 +171,9 @@ export class Canvas {
168171
return pixels;
169172
}
170173

174+
/**
175+
* Returns the Rendering Context of the canvas
176+
*/
171177
getContext(type: "2d"): CanvasRenderingContext2D;
172178
getContext(type: string): CanvasRenderingContext2D | null {
173179
switch (type) {
@@ -179,6 +185,9 @@ export class Canvas {
179185
}
180186
}
181187

188+
/**
189+
* Resizes the Canvas to the specified dimensions
190+
*/
182191
resize(width: number, height: number) {
183192
if (this.#width === width && this.#height === height) return;
184193
sk_canvas_set_size(this.#ptr, width, height);

src/context2d.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export type GlobalCompositeOperation = keyof typeof CGlobalCompositeOperation;
188188
export type ImageSmoothingQuality = keyof typeof CImageSmoothingQuality;
189189

190190
const METRICS = new Float32Array(7);
191-
const METRICS_PTR = Number(Deno.UnsafePointer.of(METRICS));
191+
const METRICS_PTR = Deno.UnsafePointer.of(METRICS);
192192

193193
export type Style = string | CanvasGradient | CanvasPattern;
194194

@@ -259,7 +259,7 @@ export class CanvasRenderingContext2D {
259259
constructor(canvas: Canvas, ptr: Deno.PointerValue) {
260260
this.#canvas = canvas;
261261
this.#ptr = ptr;
262-
if (this.#ptr === 0) {
262+
if (this.#ptr === null) {
263263
throw new Error("Failed to create context");
264264
}
265265
}
@@ -291,7 +291,7 @@ export class CanvasRenderingContext2D {
291291
y,
292292
maxWidth ?? 100_000,
293293
1,
294-
0,
294+
null,
295295
)
296296
) {
297297
throw new Error("failed to fill text");
@@ -309,7 +309,7 @@ export class CanvasRenderingContext2D {
309309
y,
310310
maxWidth ?? 100_000,
311311
0,
312-
0,
312+
null,
313313
)
314314
) {
315315
throw new Error("failed to stroke text");
@@ -773,13 +773,16 @@ export class CanvasRenderingContext2D {
773773
const pathptr =
774774
typeof path === "object" && path !== null && path instanceof Path2D
775775
? path._unsafePointer
776-
: 0;
776+
: null;
777777
const irule = (typeof path === "string" ? path : rule) ?? "nonzero";
778778
sk_context_fill(this.#ptr, pathptr, irule === "evenodd" ? 1 : 0);
779779
}
780780

781781
stroke(path?: Path2D) {
782-
sk_context_stroke(this.#ptr, path ? path._unsafePointer : 0);
782+
sk_context_stroke(
783+
this.#ptr,
784+
path ? path._unsafePointer : null,
785+
);
783786
}
784787

785788
drawFocusIfNeeded() {
@@ -800,7 +803,7 @@ export class CanvasRenderingContext2D {
800803
) {
801804
const pathptr = typeof path === "object" && path !== null
802805
? path._unsafePointer
803-
: 0;
806+
: null;
804807
const fillRuleStr = typeof path === "string" ? path : fillRule;
805808
const ifillRule = fillRuleStr === "evenodd" ? 1 : 0;
806809
sk_context_clip(this.#ptr, pathptr, ifillRule);
@@ -825,7 +828,7 @@ export class CanvasRenderingContext2D {
825828
): boolean {
826829
const pathptr = typeof path === "object" && path !== null
827830
? path._unsafePointer
828-
: 0;
831+
: null;
829832
const ifillRule = (typeof y === "string" ? y : fillRule) === "evenodd"
830833
? 1
831834
: 0;
@@ -854,7 +857,7 @@ export class CanvasRenderingContext2D {
854857
): boolean {
855858
const pathptr = typeof path === "object" && path !== null
856859
? path._unsafePointer
857-
: 0;
860+
: null;
858861
return sk_context_is_point_in_stroke(
859862
this.#ptr,
860863
typeof path === "number" ? path : x,
@@ -978,7 +981,7 @@ export class CanvasRenderingContext2D {
978981
asw?: number,
979982
ash?: number,
980983
) {
981-
if (image instanceof Image && image._unsafePointer === 0) {
984+
if (image instanceof Image && image._unsafePointer === null) {
982985
return;
983986
}
984987
const dx = asx ?? adx;
@@ -991,8 +994,8 @@ export class CanvasRenderingContext2D {
991994
const sh = ash === undefined ? image.height : adh ?? image.height;
992995
sk_context_draw_image(
993996
this.#ptr,
994-
image instanceof Canvas ? image._unsafePointer : 0,
995-
image instanceof Image ? image._unsafePointer : 0,
997+
image instanceof Canvas ? image._unsafePointer : null,
998+
image instanceof Image ? image._unsafePointer : null,
996999
dx,
9971000
dy,
9981001
dw,

src/ffi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ const {
979979
) => ArrayBuffer;
980980
op_base64_encode: (buf: Uint8Array) => string;
981981
op_base64_decode: (base64: string) => Uint8Array;
982+
// deno-lint-ignore no-explicit-any
982983
} = (Deno as any)[(Deno as any).internal].core.ops;
983984

984985
export function cstr(str: string) {

src/filter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function parseFilterString(filter: string): Filter[] {
9999
let state: "fn" | "args" = "fn";
100100
let fn = "";
101101
let argc = 0;
102+
// deno-lint-ignore no-explicit-any
102103
let argv: any[] = [];
103104

104105
let i = 0;
@@ -137,6 +138,7 @@ export function parseFilterString(filter: string): Filter[] {
137138
continue;
138139
}
139140

141+
// deno-lint-ignore no-inner-declarations
140142
function parsePixel() {
141143
let numPart = "";
142144
let unitPart = "";
@@ -182,6 +184,7 @@ export function parseFilterString(filter: string): Filter[] {
182184
}
183185
}
184186

187+
// deno-lint-ignore no-inner-declarations
185188
function parsePercentage() {
186189
if (!arg.match(/^[0-9\.]+%?$/)) {
187190
throw new Error(`Invalid percentage: ${arg}`);
@@ -197,6 +200,7 @@ export function parseFilterString(filter: string): Filter[] {
197200
return num / 100;
198201
}
199202

203+
// deno-lint-ignore no-inner-declarations
200204
function parseAngle() {
201205
if (arg.endsWith("deg")) {
202206
const num = parseFloat(arg.slice(0, arg.length - 3));
@@ -227,6 +231,7 @@ export function parseFilterString(filter: string): Filter[] {
227231
}
228232
}
229233

234+
// deno-lint-ignore no-explicit-any
230235
let v: any = undefined;
231236

232237
switch (fn) {

src/image.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const SK_IMAGE_FINALIZER = new FinalizationRegistry(
1717
export type ImageSource = Uint8Array | string;
1818

1919
export class Image extends EventTarget {
20-
#token: { ptr: Deno.PointerValue } = { ptr: 0 };
21-
#ptr: Deno.PointerValue = 0;
20+
#token: { ptr: Deno.PointerValue } = { ptr: null };
21+
#ptr: Deno.PointerValue = null;
2222
#src?: ImageSource;
2323

2424
get _unsafePointer() {
@@ -35,16 +35,16 @@ export class Image extends EventTarget {
3535
}
3636

3737
set src(data: ImageSource | undefined) {
38-
if (this.#ptr !== 0) {
38+
if (this.#ptr !== null) {
3939
sk_image_destroy(this.#ptr);
4040
SK_IMAGE_FINALIZER.unregister(this.#token);
41-
this.#ptr = 0;
41+
this.#ptr = null;
4242
}
4343

4444
if (data === undefined) {
4545
this.#src = undefined;
46-
this.#ptr = 0;
47-
this.#token.ptr = 0;
46+
this.#ptr = null;
47+
this.#token.ptr = null;
4848
return;
4949
}
5050

@@ -79,7 +79,7 @@ export class Image extends EventTarget {
7979
? sk_image_from_encoded(data, data.byteLength)
8080
: sk_image_from_file(cstr(data));
8181

82-
if (this.#ptr === 0) {
82+
if (this.#ptr === null) {
8383
const error = new Error("Failed to load image");
8484
queueMicrotask(() => {
8585
this.dispatchEvent(
@@ -94,7 +94,7 @@ export class Image extends EventTarget {
9494
this.#token.ptr = this.#ptr;
9595
this.#src = data;
9696

97-
if (this.#ptr !== 0) {
97+
if (this.#ptr !== null) {
9898
SK_IMAGE_FINALIZER.register(this, this.#ptr, this.#token);
9999
}
100100

@@ -114,6 +114,7 @@ export class Image extends EventTarget {
114114
return this.#onerror;
115115
}
116116

117+
// deno-lint-ignore adjacent-overload-signatures
117118
set onload(fn: EventListenerOrEventListenerObject | undefined) {
118119
if (this.#onload) {
119120
this.removeEventListener("load", this.#onload);
@@ -122,6 +123,7 @@ export class Image extends EventTarget {
122123
if (fn) this.addEventListener("load", fn);
123124
}
124125

126+
// deno-lint-ignore adjacent-overload-signatures
125127
set onerror(fn: EventListenerOrEventListenerObject | undefined) {
126128
if (this.#onerror) {
127129
this.removeEventListener("error", this.#onerror);
@@ -148,17 +150,17 @@ export class Image extends EventTarget {
148150
}
149151

150152
get width() {
151-
if (this._unsafePointer === 0) return 0;
153+
if (this._unsafePointer === null) return 0;
152154
return sk_image_width(this.#ptr);
153155
}
154156

155157
get height() {
156-
if (this._unsafePointer === 0) return 0;
158+
if (this._unsafePointer === null) return 0;
157159
return sk_image_height(this.#ptr);
158160
}
159161

160162
[Symbol.for("Deno.customInspect")]() {
161-
if (this._unsafePointer === 0) {
163+
if (this._unsafePointer === null) {
162164
return `Image { pending, src: ${Deno.inspect(this.src)} }`;
163165
}
164166
return `Image { width: ${this.width}, height: ${this.height} }`;

src/parse_font.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const sizeFamilyRe = new RegExp(
2929
* Cache font parsing.
3030
*/
3131

32+
// deno-lint-ignore no-explicit-any
3233
const cache: Record<string, any> = {};
3334

3435
const defaultHeight = 16; // pt, common browser default
3536

3637
/**
3738
* Parse font `str`.
3839
*/
40+
// deno-lint-ignore no-explicit-any
3941
export function parseFont(str: string): any {
4042
// Cached
4143
if (cache[str]) return cache[str];
@@ -45,6 +47,7 @@ export function parseFont(str: string): any {
4547
if (!sizeFamily) return; // invalid
4648

4749
// Default values and required properties
50+
// deno-lint-ignore no-explicit-any
4851
const font: any = {
4952
weight: "normal",
5053
style: "normal",

src/path2d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FillRule } from "./context2d.ts";
2+
import { DOMMatrix } from "./dommatrix.ts";
23
import ffi, { cstr } from "./ffi.ts";
34

45
const {
@@ -82,7 +83,7 @@ export class Path2D {
8283
: typeof path === "object" && path !== null && path instanceof Path2D
8384
? sk_path_create_copy(path._unsafePointer)
8485
: sk_path_create();
85-
if (this.#ptr === 0) {
86+
if (this.#ptr === null) {
8687
throw new Error("Failed to create path");
8788
}
8889
PATH_FINALIZER.register(this, this.#ptr);
@@ -200,7 +201,7 @@ export class Path2D {
200201
toSVGString() {
201202
const skstr = sk_path_to_svg(this.#ptr, OUT_PTR_U8, OUT_SIZE_U8);
202203
const buffer = Deno.UnsafePointerView.getArrayBuffer(
203-
OUT_PTR[0],
204+
Deno.UnsafePointer.create(OUT_PTR[0])!,
204205
OUT_SIZE[0],
205206
);
206207
const str = new TextDecoder().decode(buffer);
@@ -248,7 +249,7 @@ export class Path2D {
248249
]);
249250
sk_path_add_path_buf(this.#ptr, path._unsafePointer, TU8);
250251
} else {
251-
sk_path_add_path_ptr(this.#ptr, path._unsafePointer, 0);
252+
sk_path_add_path_ptr(this.#ptr, path._unsafePointer, null);
252253
}
253254
}
254255
}

src/pattern.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class CanvasPattern {
3030
}
3131

3232
constructor(image: CanvasPatternImage, repetition: CanvasPatternRepeat) {
33-
if (image._unsafePointer === 0) throw new Error("Image not loaded");
33+
if (image._unsafePointer === null) throw new Error("Image not loaded");
3434
this.#ptr = sk_pattern_new_image(image._unsafePointer, repeat[repetition]);
3535
PATTERN_FINALIZER.register(this, this.#ptr);
3636
}

0 commit comments

Comments
 (0)