-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples for extern and packed struct (#277)
- Loading branch information
1 parent
310bc82
commit a40b075
Showing
11 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
32 | ||
24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = .{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ state: false, number: 123456789000000n } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = .{}; |