Skip to content

Commit 4df5002

Browse files
authored
feat: generate classes from metadata (#15)
* feat: generate classes from metadata * some more work * progress * use class definitions from metadata for registerClass * finally fix the protocol bug * clean up * add support for protocol objects, untyped returns, and a metal compute example * cleanup
1 parent 027a25b commit 4df5002

42 files changed

Lines changed: 1955 additions & 1055 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ set(LIB_SOURCE_FILES
9999
src/JSObject.mm
100100
src/Enum.mm
101101
src/Variable.mm
102+
src/Object.mm
103+
src/CustomClass.mm
104+
src/CFunction.mm
102105
)
103106

104107
add_library(

‎examples/appkit.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "objc";
22

33
export class ApplicationDelegate extends NSObject {
4-
static protocols = ["NSApplicationDelegate"];
4+
static protocols = [NSApplicationDelegate];
55

66
static {
77
objc.registerClass(this);
@@ -48,7 +48,7 @@ export class ApplicationDelegate extends NSObject {
4848
}
4949

5050
export class Window extends NSWindow {
51-
static protocols = ["NSWindowDelegate"];
51+
static protocols = [NSWindowDelegate];
5252

5353
static {
5454
objc.registerClass(this);

‎examples/class.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import "objc";
2+
3+
class MyClass extends NSObject {
4+
static {
5+
objc.registerClass(this);
6+
}
7+
8+
init() {
9+
super.init();
10+
console.log("MyClass.init()", this);
11+
return this;
12+
}
13+
}
14+
15+
const myClass = MyClass.new();
16+
console.log(myClass);

‎examples/foundation.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import "objc";
22

3-
console.log(NSString.stringWithUTF8String("Hello, macOS").length);
3+
console.log(NSObject.new());
4+
console.log(NSString.stringWithUTF8String("Hello, macOS").constructor);
45

56
// const device = MTLCreateSystemDefaultDevice();
6-
// console.log(device);
7+
// console.log(device, device.constructor.name);
78

89
// const queue = device.newCommandQueue();
910
// console.log(queue);

‎examples/gamecontroller.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "../index.js";
2+
3+
objc.import("GameController");
4+
5+
console.log(GCController.controllers().count);

‎examples/metal.js‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import "../index.js";
2+
3+
objc.import("Metal");
4+
5+
const arrayLength = 1 << 24;
6+
const bufferSize = arrayLength * Float32Array.BYTES_PER_ELEMENT;
7+
8+
function MTLSizeMake(width, height, depth) {
9+
return { width, height, depth };
10+
}
11+
12+
function getFloat32Array(ptr, size) {
13+
const ab = objc.getArrayBuffer(ptr, size);
14+
return new Float32Array(ab);
15+
}
16+
17+
class MetalAdder {
18+
constructor(device) {
19+
this.device = device;
20+
21+
const library = device.newLibraryWithSourceOptionsError(
22+
`
23+
#include <metal_stdlib>
24+
using namespace metal;
25+
26+
kernel void add_arrays(device const float* inA,
27+
device const float* inB,
28+
device float* result,
29+
uint index [[thread_position_in_grid]])
30+
{
31+
result[index] = inA[index] + inB[index];
32+
}
33+
`,
34+
null,
35+
null,
36+
);
37+
38+
const addFunction = library.newFunctionWithName("add_arrays");
39+
40+
const error = new ObjectRef();
41+
this.pipelineState = device.newComputePipelineStateWithFunctionError(
42+
addFunction,
43+
error,
44+
);
45+
if (!this.pipelineState) {
46+
console.log(error.value);
47+
}
48+
49+
this.commandQueue = device.newCommandQueue();
50+
}
51+
52+
prepareData() {
53+
this.mBufferA = this.device.newBufferWithLengthOptions(
54+
bufferSize,
55+
MTLResourceOptions.storageModeShared,
56+
);
57+
this.mBufferB = this.device.newBufferWithLengthOptions(
58+
bufferSize,
59+
MTLResourceOptions.storageModeShared,
60+
);
61+
this.mBufferResult = this.device.newBufferWithLengthOptions(
62+
bufferSize,
63+
MTLResourceOptions.storageModeShared,
64+
);
65+
66+
this.generateRandomFloatData(this.mBufferA);
67+
this.generateRandomFloatData(this.mBufferB);
68+
}
69+
70+
sendComputeCommand() {
71+
const commandBuffer = this.commandQueue.commandBuffer();
72+
if (commandBuffer === null) {
73+
throw new Error("Failed to create command buffer");
74+
}
75+
76+
const commandEncoder = commandBuffer.computeCommandEncoder();
77+
if (commandEncoder === null) {
78+
throw new Error("Failed to create command encoder");
79+
}
80+
81+
this.encodeAddCommand(commandEncoder);
82+
83+
commandEncoder.endEncoding();
84+
85+
commandBuffer.commit();
86+
87+
commandBuffer.waitUntilCompleted();
88+
89+
this.verifyResults();
90+
}
91+
92+
encodeAddCommand(computeEncoder) {
93+
computeEncoder.setComputePipelineState(this.pipelineState);
94+
computeEncoder.setBufferOffsetAtIndex(this.mBufferA, 0, 0);
95+
computeEncoder.setBufferOffsetAtIndex(this.mBufferB, 0, 1);
96+
computeEncoder.setBufferOffsetAtIndex(this.mBufferResult, 0, 2);
97+
98+
const gridSize = MTLSizeMake(arrayLength, 1, 1);
99+
100+
const threadGroupSize = this.pipelineState.maxTotalThreadsPerThreadgroup;
101+
if (threadGroupSize > arrayLength) {
102+
threadGroupSize = arrayLength;
103+
}
104+
105+
const threadgroupSize = MTLSizeMake(threadGroupSize, 1, 1);
106+
107+
computeEncoder.dispatchThreadsThreadsPerThreadgroup(
108+
gridSize,
109+
threadgroupSize,
110+
);
111+
}
112+
113+
generateRandomFloatData(buffer) {
114+
const contents = getFloat32Array(buffer.contents(), bufferSize);
115+
for (let i = 0; i < arrayLength; i++) {
116+
contents[i] = Math.floor(Math.random() * 100);
117+
}
118+
}
119+
120+
verifyResults() {
121+
const a = getFloat32Array(this.mBufferA.contents(), bufferSize);
122+
const b = getFloat32Array(this.mBufferB.contents(), bufferSize);
123+
const result = getFloat32Array(this.mBufferResult.contents(), bufferSize);
124+
125+
for (let i = 0; i < arrayLength; i++) {
126+
if (a[i] + b[i] !== result[i]) {
127+
console.log("Results don't match!", i, a[i], b[i], result[i]);
128+
return;
129+
}
130+
}
131+
132+
console.log("Results match!");
133+
}
134+
}
135+
136+
const device = MTLCopyAllDevices().firstObject;
137+
138+
const adder = new MetalAdder(device);
139+
140+
adder.prepareData();
141+
142+
adder.sendComputeCommand();

‎examples/mlcompute.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ inference.compileWithOptionsDevice(
5555
device,
5656
);
5757

58-
const callback = objc.registerBlock("v@@d", (output, error, time) => {
59-
console.log(output, error, time);
60-
});
61-
6258
inference.executeWithInputsDataBatchSizeOptionsCompletionHandler(
6359
{ input: dataInput },
6460
batchSize,
6561
MLCExecutionOptions.synchronous,
66-
callback,
62+
(output, error, time) => {
63+
console.log(output, error, time);
64+
},
6765
);

‎examples/split_view.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "objc";
22

33
export class ApplicationDelegate extends NSObject {
4-
static protocols = ["NSApplicationDelegate"];
4+
static protocols = [NSApplicationDelegate];
55

66
static {
77
objc.registerClass(this);
@@ -59,7 +59,7 @@ export class Node {
5959
}
6060

6161
export class SidebarViewController extends NSViewController {
62-
static protocols = ["NSOutlineViewDelegate", "NSOutlineViewDataSource"];
62+
static protocols = [NSOutlineViewDelegate, NSOutlineViewDataSource];
6363

6464
static {
6565
objc.registerClass(this);
@@ -245,7 +245,7 @@ export class ContentViewController extends NSViewController {
245245
}
246246

247247
export class SplitViewController extends NSSplitViewController {
248-
static protocols = ["NSToolbarDelegate"];
248+
static protocols = [NSToolbarDelegate];
249249

250250
static {
251251
objc.registerClass(this);

‎examples/spritekit.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import "objc";
33
objc.import("SpriteKit");
44

55
export class ApplicationDelegate extends NSObject {
6-
static protocols = ["NSApplicationDelegate", "NSWindowDelegate"];
6+
static protocols = [NSApplicationDelegate, NSWindowDelegate];
77

88
static {
99
objc.registerClass(this);
@@ -47,7 +47,7 @@ export class ApplicationDelegate extends NSObject {
4747
}
4848

4949
export class BattlefieldScene extends SKScene {
50-
static protocols = ["SKPhysicsContactDelegate"];
50+
static protocols = [SKPhysicsContactDelegate];
5151

5252
static {
5353
objc.registerClass(this);

‎examples/view_controller.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "objc";
22

33
export class ApplicationDelegate extends NSObject {
4-
static protocols = ["NSApplicationDelegate", "NSWindowDelegate"];
4+
static protocols = [NSApplicationDelegate, NSWindowDelegate];
55

66
static {
77
objc.registerClass(this);
@@ -20,6 +20,7 @@ export class ApplicationDelegate extends NSObject {
2020
appMenu.addItemWithTitleActionKeyEquivalent("Quit", "terminate:", "q");
2121

2222
const controller = ViewController.new();
23+
globalThis.vc = controller;
2324
const window = NSWindow.windowWithContentViewController(controller);
2425

2526
window.title = "NativeScript for macOS";

0 commit comments

Comments
 (0)