@retrovm/nobj 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +23 -11
  2. package/nobj.ts +24 -15
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -48,14 +48,8 @@ await Bun.write('assets.o', buf)
48
48
  Link normally:
49
49
 
50
50
  ```bash
51
- # macOS
51
+ # Any Os
52
52
  clang main.c assets.o -o app
53
-
54
- # Linux
55
- clang main.c assets.o -o app
56
-
57
- # Windows
58
- clang-cl main.c assets.obj -o app.exe
59
53
  ```
60
54
 
61
55
  Reference from C with no runtime overhead:
@@ -93,7 +87,7 @@ const buf = encodeSymbols([
93
87
 
94
88
  | TypeScript type | C declaration | Notes |
95
89
  |-----------------|--------------------------|----------------------------------------|
96
- | `Buffer` | `const uint8_t[]` | Raw bytes, no transformation |
90
+ | `Uint8Array` | `const uint8_t[]` | Raw bytes, no transformation |
97
91
  | `string` | `const char[]` | ASCII, null-terminated (except macOS) |
98
92
  | `number` | `const double` | IEEE 754, 8 bytes |
99
93
 
@@ -104,13 +98,13 @@ By default both platform and architecture are inferred from the running process.
104
98
  ```ts
105
99
  encodeSymbols(symbols, 'arm64', 'linux') // Linux/arm64
106
100
  encodeSymbols(symbols, 'x64', 'win32') // Windows/x64
107
- encodeSymbols(symbols, 'arm64', 'darwin') // macOS/arm64
101
+ encodeSymbols(symbols, 'arm64', 'macos') // macOS/arm64
108
102
  ```
109
103
 
110
104
  | `TargetPlatform` | Format | Section |
111
105
  |------------------|--------|-----------|
112
106
  | `'win32'` | COFF | `.rdata` |
113
- | `'darwin'` | Mach-O | `__const` |
107
+ | `'macos'` | Mach-O | `__const` |
114
108
  | `'linux'` | ELF64 | `.rodata` |
115
109
 
116
110
  | `TargetArch` | COFF machine | Mach-O cputype |
@@ -132,4 +126,22 @@ The generated files are minimal but correct — accepted by `clang`, `lld` and `
132
126
 
133
127
  ## License
134
128
 
135
- 2026 - MIT © Juan Carlos González Amestoy
129
+ Copyright (c) 2026 Juan Carlos González Amestoy
130
+
131
+ Permission is hereby granted, free of charge, to any person obtaining a copy
132
+ of this software and associated documentation files (the "Software"), to deal
133
+ in the Software without restriction, including without limitation the rights
134
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
135
+ copies of the Software, and to permit persons to whom the Software is
136
+ furnished to do so, subject to the following conditions:
137
+
138
+ The above copyright notice and this permission notice shall be included in all
139
+ copies or substantial portions of the Software.
140
+
141
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
142
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
143
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
144
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
145
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
146
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
147
+ SOFTWARE.
package/nobj.ts CHANGED
@@ -18,14 +18,14 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  SOFTWARE.*/
20
20
 
21
- export type SymbolValue = Buffer | string | number;
21
+ export type SymbolValue = Uint8Array | string | number;
22
22
 
23
23
  export interface ObjSymbol {
24
24
  name: string;
25
25
  obj: SymbolValue;
26
26
  }
27
27
 
28
- export type TargetPlatform = 'win32' | 'darwin' | 'linux';
28
+ export type TargetPlatform = 'win32' | 'macos' | 'linux';
29
29
  export type TargetArch = 'x64' | 'arm64';
30
30
 
31
31
  // ─── Helpers ─────────────────────────────────────────────────────────────────
@@ -54,6 +54,15 @@ function hostArch(): TargetArch {
54
54
  throw new Error(`Unsupported host architecture: ${process.arch}`);
55
55
  }
56
56
 
57
+ export function hostPlatform(): TargetPlatform {
58
+ switch (process.platform) {
59
+ case 'win32': return 'win32';
60
+ case 'darwin': return 'macos';
61
+ case 'linux': return 'linux';
62
+ default: throw new Error(`Unsupported platform: ${process.platform}`);
63
+ }
64
+ }
65
+
57
66
  // ═══════════════════════════════════════════════════════════════════════════════
58
67
  // Windows — COFF
59
68
  // ═══════════════════════════════════════════════════════════════════════════════
@@ -88,7 +97,7 @@ function doObjectWindows(symbols: ObjSymbol[], arch: TargetArch): Buffer {
88
97
  for (const { name, obj } of symbols) {
89
98
  strTabSz += name.length + 1;
90
99
  directives.push(` /EXPORT:${name},DATA`);
91
- if (Buffer.isBuffer(obj)) dataSz += al(obj.length, 8);
100
+ if (obj instanceof Uint8Array) dataSz += al(obj.length, 8);
92
101
  else if (typeof obj === 'string') dataSz += al(obj.length + 1, 8);
93
102
  else if (typeof obj === 'number') dataSz += 8;
94
103
  else throw new Error('Invalid symbol type');
@@ -119,8 +128,8 @@ function doObjectWindows(symbols: ObjSymbol[], arch: TargetArch): Buffer {
119
128
  tsd.write(name, strOff, 'ascii');
120
129
  strOff += name.length + 1;
121
130
 
122
- if (Buffer.isBuffer(obj)) {
123
- obj.copy(dsd, dataOff);
131
+ if (obj instanceof Uint8Array) {
132
+ dsd.set(obj, dataOff);
124
133
  dataOff += al(obj.length, 8);
125
134
  } else if (typeof obj === 'string') {
126
135
  dsd.write(obj, dataOff, 'ascii');
@@ -225,7 +234,7 @@ function doObjectMacOS(symbols: ObjSymbol[], arch: TargetArch): Buffer {
225
234
  let dataSz = 0, strTabSz = 1, symTabSz = 0;
226
235
 
227
236
  for (const { name, obj } of symbols) {
228
- if (Buffer.isBuffer(obj)) dataSz += al(obj.length, 16);
237
+ if (obj instanceof Uint8Array) dataSz += al(obj.length, 16);
229
238
  else if (typeof obj === 'string') dataSz += al(obj.length, 16); // raw bytes, no null
230
239
  else if (typeof obj === 'number') dataSz += al(8, 16); // = 16
231
240
  else throw new Error('Invalid symbol type');
@@ -244,8 +253,8 @@ function doObjectMacOS(symbols: ObjSymbol[], arch: TargetArch): Buffer {
244
253
  const { name, obj } = symbols[k]!;
245
254
  const valueInSection = dataOff;
246
255
 
247
- if (Buffer.isBuffer(obj)) {
248
- obj.copy(dt, dataOff);
256
+ if (obj instanceof Uint8Array) {
257
+ dt.set(obj, dataOff);
249
258
  dataOff += al(obj.length, 16);
250
259
  } else if (typeof obj === 'string') {
251
260
  dt.write(obj, dataOff, 'ascii');
@@ -377,7 +386,7 @@ function doObjectLinux(symbols: ObjSymbol[], arch: TargetArch): Buffer {
377
386
 
378
387
  for (const { name, obj } of symbols) {
379
388
  strTabSz += name.length + 1;
380
- if (Buffer.isBuffer(obj)) dataSz += al(obj.length, 8);
389
+ if (obj instanceof Uint8Array) dataSz += al(obj.length, 8);
381
390
  else if (typeof obj === 'string') dataSz += al(obj.length + 1, 8);
382
391
  else if (typeof obj === 'number') dataSz += 8;
383
392
  else throw new Error('Invalid symbol type');
@@ -405,8 +414,8 @@ function doObjectLinux(symbols: ObjSymbol[], arch: TargetArch): Buffer {
405
414
  ts.write(name, strOff, 'ascii');
406
415
  strOff += name.length + 1;
407
416
 
408
- if (Buffer.isBuffer(obj)) {
409
- obj.copy(d, dataOff);
417
+ if (obj instanceof Uint8Array) {
418
+ d.set(obj, dataOff);
410
419
  writeU64(symd, sb + EY.SIZE, obj.length);
411
420
  dataOff += al(obj.length, 8);
412
421
  } else if (typeof obj === 'string') {
@@ -493,12 +502,12 @@ function doObjectLinux(symbols: ObjSymbol[], arch: TargetArch): Buffer {
493
502
  function dispatch(
494
503
  symbols: ObjSymbol[],
495
504
  arch: TargetArch = hostArch(),
496
- platform: TargetPlatform = process.platform as TargetPlatform,
505
+ platform: TargetPlatform = hostPlatform(),
497
506
  ): Buffer {
498
507
  switch (platform) {
499
- case 'win32': return doObjectWindows(symbols, arch);
500
- case 'darwin': return doObjectMacOS(symbols, arch);
501
- case 'linux': return doObjectLinux(symbols, arch);
508
+ case 'win32': return doObjectWindows(symbols, arch);
509
+ case 'macos': return doObjectMacOS(symbols, arch);
510
+ case 'linux': return doObjectLinux(symbols, arch);
502
511
  default: throw new Error(`Unsupported platform: ${platform}`);
503
512
  }
504
513
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrovm/nobj",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Generate native linkable object files (COFF, Mach-O, ELF64) from TypeScript/Bun — zero native dependencies",
5
5
  "license": "MIT",
6
6
  "author": "Juan Carlos González Amestoy",