Repository navigation
Expand file tree
/
Copy pathdeno_ffi.js
More file actions
77 lines (63 loc) · 1.68 KB
/
Copy pathdeno_ffi.js
File metadata and controls
77 lines (63 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Just a benchmark to compare the performance of V8 Fast API and the slow API
// in case of Objective-C interop.
const {
objc_getClass,
sel_registerName,
objc_msgSend,
objc_msgSend_stringWithUTF8String,
objc_msgSend_length,
objc_msgSend_length_slow,
} = Deno.dlopen("libobjc.dylib", {
objc_getClass: {
parameters: ["buffer"],
result: "pointer",
},
sel_registerName: {
parameters: ["buffer"],
result: "pointer",
},
objc_msgSend_stringWithUTF8String: {
name: "objc_msgSend",
parameters: ["pointer", "pointer", "buffer"],
result: "pointer",
},
objc_msgSend_length: {
name: "objc_msgSend",
parameters: ["pointer", "pointer"],
result: "pointer",
},
objc_msgSend_length_slow: {
name: "objc_msgSend",
parameters: ["pointer", "pointer"],
result: "pointer",
callback: true, // disable V8 Fast API
},
objc_msgSend: {
type: "pointer",
},
}).symbols;
function cstr(str) {
return new TextEncoder().encode(str + "\0");
}
const NSString = objc_getClass(cstr("NSString"));
const stringWithUTF8String = sel_registerName(cstr("stringWithUTF8String:"));
const length = sel_registerName(cstr("length"));
const str = objc_msgSend_stringWithUTF8String(
NSString,
stringWithUTF8String,
cstr("Hello World"),
);
const fnptr = new Deno.UnsafeFnPointer(objc_msgSend, {
parameters: ["pointer", "pointer"],
result: "pointer",
});
Deno.bench("noop", () => {});
Deno.bench("[str length] (fast)", () => {
objc_msgSend_length(str, length);
});
Deno.bench("[str length] (slow, autogenerated)", () => {
objc_msgSend_length_slow(str, length);
});
Deno.bench("[str length] (slow, dynamic)", () => {
fnptr.call(str, length);
});