sysjs: automatically cast numbers to f64 & remove dead code
This commit is contained in:
parent
2de36ad75b
commit
7df12a0cae
3 changed files with 23 additions and 24 deletions
|
|
@ -70,24 +70,24 @@ pub const Context = struct {
|
|||
pub fn createPlayer(self: *Context, device: main.Device, writeFn: main.WriteFn, options: main.Player.Options) !backends.BackendPlayer {
|
||||
const context_options = js.createMap();
|
||||
defer context_options.deinit();
|
||||
context_options.set("sampleRate", js.createNumber(@intToFloat(f64, options.sample_rate)));
|
||||
context_options.set("sampleRate", js.createNumber(options.sample_rate));
|
||||
|
||||
const audio_context = js.constructType("AudioContext", &.{context_options.toValue()});
|
||||
const gain_node = audio_context.call("createGain", &.{
|
||||
js.createNumber(1),
|
||||
js.createNumber(0),
|
||||
js.createNumber(@intToFloat(f64, device.channels.len)),
|
||||
js.createNumber(device.channels.len),
|
||||
}).view(.object);
|
||||
const process_node = audio_context.call("createScriptProcessor", &.{
|
||||
js.createNumber(channel_size),
|
||||
js.createNumber(@intToFloat(f64, device.channels.len)),
|
||||
js.createNumber(device.channels.len),
|
||||
}).view(.object);
|
||||
|
||||
var player = try self.allocator.create(Player);
|
||||
errdefer self.allocator.destroy(player);
|
||||
|
||||
var captures = try self.allocator.alloc(js.Value, 1);
|
||||
captures[0] = js.createNumber(@intToFloat(f64, @ptrToInt(player)));
|
||||
captures[0] = js.createNumber(@ptrToInt(player));
|
||||
|
||||
const document = js.global().get("document").view(.object);
|
||||
defer document.deinit();
|
||||
|
|
@ -190,7 +190,7 @@ pub const Player = struct {
|
|||
self.buf_js.copyBytes(self.buf[i * channel_size_bytes .. (i + 1) * channel_size_bytes]);
|
||||
const buf_f32_js = js.constructType("Float32Array", &.{ self.buf_js.get("buffer"), self.buf_js.get("byteOffset"), js.createNumber(channel_size) });
|
||||
defer buf_f32_js.deinit();
|
||||
_ = output_buffer.call("copyToChannel", &.{ buf_f32_js.toValue(), js.createNumber(@intToFloat(f64, i)) });
|
||||
_ = output_buffer.call("copyToChannel", &.{ buf_f32_js.toValue(), js.createNumber(i) });
|
||||
}
|
||||
|
||||
return js.createUndefined();
|
||||
|
|
|
|||
|
|
@ -131,7 +131,6 @@ const sysjs = {
|
|||
default:
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case "number":
|
||||
return 1;
|
||||
case "boolean":
|
||||
|
|
@ -173,27 +172,21 @@ const sysjs = {
|
|||
}
|
||||
},
|
||||
|
||||
readObject(block, memory) {
|
||||
readObject(block) {
|
||||
switch (block.getU8(0)) {
|
||||
case 0:
|
||||
case 6:
|
||||
return values[block.getU64(8)];
|
||||
break;
|
||||
case 1:
|
||||
return block.getF64(8);
|
||||
break;
|
||||
case 2:
|
||||
return Boolean(block.getU8(8));
|
||||
break;
|
||||
case 3:
|
||||
return values[block.getU64(8)];
|
||||
break;
|
||||
case 4:
|
||||
return null;
|
||||
break;
|
||||
case 5:
|
||||
return undefined;
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -232,8 +225,7 @@ const sysjs = {
|
|||
zigSetProperty(id, name, len, set_ptr) {
|
||||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
values[id][memory.getString(name, len)] = sysjs.readObject(
|
||||
memory.slice(set_ptr),
|
||||
memory
|
||||
memory.slice(set_ptr)
|
||||
);
|
||||
},
|
||||
|
||||
|
|
@ -249,7 +241,7 @@ const sysjs = {
|
|||
|
||||
zigSetIndex(id, index, set_ptr) {
|
||||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
values[id][index] = sysjs.readObject(memory.slice(set_ptr), memory);
|
||||
values[id][index] = sysjs.readObject(memory.slice(set_ptr));
|
||||
},
|
||||
|
||||
zigDeleteIndex(id, index) {
|
||||
|
|
@ -289,15 +281,15 @@ const sysjs = {
|
|||
|
||||
zigValueEqual(val, other) {
|
||||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
const val_js = sysjs.readObject(memory.slice(val), memory);
|
||||
const other_js = sysjs.readObject(memory.slice(other), memory);
|
||||
const val_js = sysjs.readObject(memory.slice(val));
|
||||
const other_js = sysjs.readObject(memory.slice(other));
|
||||
return val_js === other_js;
|
||||
},
|
||||
|
||||
zigValueInstanceOf(val, other) {
|
||||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
const val_js = sysjs.readObject(memory.slice(val), memory);
|
||||
const other_js = sysjs.readObject(memory.slice(other), memory);
|
||||
const val_js = sysjs.readObject(memory.slice(val));
|
||||
const other_js = sysjs.readObject(memory.slice(other));
|
||||
return val_js instanceof other_js;
|
||||
},
|
||||
|
||||
|
|
@ -305,7 +297,7 @@ const sysjs = {
|
|||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
let argv = [];
|
||||
for (let i = 0; i < args_len; i += 1) {
|
||||
argv.push(sysjs.readObject(memory.slice(args + i * 16), memory));
|
||||
argv.push(sysjs.readObject(memory.slice(args + i * 16)));
|
||||
}
|
||||
|
||||
let result = func.apply(this_param, argv);
|
||||
|
|
@ -349,7 +341,7 @@ const sysjs = {
|
|||
let memory = new MemoryBlock(sysjs.wasm.exports.memory.buffer);
|
||||
let argv = [];
|
||||
for (let i = 0; i < args_len; i += 1) {
|
||||
argv.push(sysjs.readObject(memory.slice(args + i * 16), memory));
|
||||
argv.push(sysjs.readObject(memory.slice(args + i * 16)));
|
||||
}
|
||||
|
||||
return sysjs.addValue(new values[id](...argv));
|
||||
|
|
|
|||
|
|
@ -228,8 +228,15 @@ pub fn createString(string: []const u8) String {
|
|||
return .{ .ref = js.zigCreateString(string.ptr, @intCast(u32, string.len)) };
|
||||
}
|
||||
|
||||
pub fn createNumber(num: f64) Value {
|
||||
return .{ .tag = .num, .val = .{ .num = num } };
|
||||
pub fn createNumber(num: anytype) Value {
|
||||
switch (@typeInfo(@TypeOf(num))) {
|
||||
.Int,
|
||||
.Float,
|
||||
.ComptimeInt,
|
||||
.ComptimeFloat,
|
||||
=> return .{ .tag = .num, .val = .{ .num = std.math.lossyCast(f64, num) } },
|
||||
else => @compileError("expected integer, found " ++ @typeName(@TypeOf(num))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn createBool(val: bool) Value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue