Skip to content

Commit

Permalink
Added examples for extern and packed struct (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
chung-leong committed Apr 11, 2024
1 parent 310bc82 commit a40b075
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/examples/extern-struct-example-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ext_struct, reg_struct } from './extern-struct-example-1.zig';

console.log('Extern:');
console.log(ext_struct.dataView.getInt16(0, true));
console.log(ext_struct.dataView.getBigInt64(8, true));
console.log('Regular (wrong):');
console.log(reg_struct.dataView.getInt16(0, true));
console.log(reg_struct.dataView.getBigInt64(8, true));
console.log('Regular (correct):');
console.log(reg_struct.dataView.getInt16(8, true));
console.log(reg_struct.dataView.getBigInt64(0, true));
9 changes: 9 additions & 0 deletions docs/examples/extern-struct-example-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Extern:
123
456n
Regular (wrong):
456
123n
Regular (correct):
123
456n
12 changes: 12 additions & 0 deletions docs/examples/extern-struct-example-1.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub const ExternStruct = extern struct {
small_int: i16,
big_int: i64,
};

pub const RegularStruct = struct {
small_int: i16,
big_int: i64,
};

pub const ext_struct: ExternStruct = .{ .small_int = 123, .big_int = 456 };
pub const reg_struct: RegularStruct = .{ .small_int = 123, .big_int = 456 };
4 changes: 4 additions & 0 deletions docs/examples/extern-struct-example-1b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ext_struct, reg_struct } from './extern-struct-example-1.zig';

console.log(ext_struct.dataView.byteLength);
console.log(reg_struct.dataView.byteLength);
2 changes: 2 additions & 0 deletions docs/examples/extern-struct-example-1b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
32
24
6 changes: 6 additions & 0 deletions docs/examples/packed-struct-example-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { pac_struct, reg_struct } from './packed-struct-example-1.zig';

console.log(pac_struct.valueOf());
console.log(reg_struct.valueOf());
console.log(pac_struct.dataView.byteLength);
console.log(reg_struct.dataView.byteLength);
20 changes: 20 additions & 0 deletions docs/examples/packed-struct-example-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
state_a: false,
state_b: true,
state_c: false,
state_d: false,
state_e: false,
state_f: false,
state_g: false
}
{
state_a: false,
state_b: true,
state_c: false,
state_d: false,
state_e: false,
state_f: false,
state_g: false
}
1
7
22 changes: 22 additions & 0 deletions docs/examples/packed-struct-example-1.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub const PackedStruct = packed struct {
state_a: bool = false,
state_b: bool = true,
state_c: bool = false,
state_d: bool = false,
state_e: bool = false,
state_f: bool = false,
state_g: bool = false,
};

pub const RegularStruct = struct {
state_a: bool = false,
state_b: bool = true,
state_c: bool = false,
state_d: bool = false,
state_e: bool = false,
state_f: bool = false,
state_g: bool = false,
};

pub const pac_struct: PackedStruct = .{};
pub const reg_struct: RegularStruct = .{};
3 changes: 3 additions & 0 deletions docs/examples/packed-struct-example-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { weird_struct } from './packed-struct-example-2.zig';

console.log(weird_struct.valueOf());
1 change: 1 addition & 0 deletions docs/examples/packed-struct-example-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ state: false, number: 123456789000000n }
6 changes: 6 additions & 0 deletions docs/examples/packed-struct-example-2.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const WeirdStruct = packed struct {
state: bool = false,
number: u128 = 123456789000000,
};

pub const weird_struct: WeirdStruct = .{};

0 comments on commit a40b075

Please sign in to comment.