Limitation ‣ No shared memory in Electron - chung-leong/zigar GitHub Wiki
Electron no longer supports
external buffers
(see issue). The lack of shared memory makes
it difficult to access Zig memory from JavaScript. Zigar uses a workaround to allow variables to
function correctly. When the special properties dataView
and typedArray
bypass this workaround so they don't behave as you'd expect.
Consider the following example:
const std = @import("std");
pub var array: [4]i32 = .{ 1, 2, 3, 4 };
pub fn changeArray(index: usize, value: i32) void {
array[index] = value;
}
require('node-zigar');
const test = require('../test.zig');
test.changeArray(0, 888);
const { typedArray } = test.array;
console.log(typedArray);
test.changeArray(1, 777);
console.log(typedArray);
console.log(test.array.typedArray);
console.log(typedArray);
Int32Array(4) [ 888, 2, 3, 4 ]
Int32Array(4) [ 888, 2, 3, 4 ]
Int32Array(4) [ 888, 777, 3, 4 ]
Int32Array(4) [ 888, 777, 3, 4 ]
Here test.array
is not in possession of a shared memory buffer. It has a regular ArrayBuffer
instead. When its typedArray
property is accessed, values from the array in Zig are copied into
this buffer. It then becomes out-of-date when array is modified through changeArray()
. That's
why the second line in the output is still Int32Array(4) [ 888, 2, 3, 4 ]
. When typedArray
is
access again, the buffer is sync'ed once more and we finally see the change.
Modifying elements of typedArray
has no effect on the array in Zig:
test.array.typedArray[3] = 333;
console.log(test.array.typedArray);
Int32Array(4) [ 888, 777, 3, 4 ]
An assignment to the typedArray
does work:
test.array.typedArray = new Int32Array([ 4, 3, 2, 1 ]);
Int32Array(4) [ 4, 3, 2, 1 ]