haku1 API cleanups (removing 2 suffix)
This commit is contained in:
parent
00a48527ca
commit
c80cd1c7fe
6 changed files with 82 additions and 77 deletions
|
@ -370,10 +370,8 @@ unsafe extern "C" fn haku_pixmap_stroke(
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
// v2 compile-only support
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_compile_brush2(
|
unsafe extern "C" fn haku_compile_brush(
|
||||||
instance: *mut Instance,
|
instance: *mut Instance,
|
||||||
code_len: u32,
|
code_len: u32,
|
||||||
code: *const u8,
|
code: *const u8,
|
||||||
|
@ -480,72 +478,78 @@ unsafe extern "C" fn haku_compile_brush2(
|
||||||
StatusCode::Ok
|
StatusCode::Ok
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn compile_result<'a>(instance: *const Instance) -> &'a CompileResult {
|
unsafe fn unwrap_compile_result<'a>(instance: *const Instance) -> &'a CompileResult {
|
||||||
let cr = (*instance).compile_result2.as_ref().unwrap();
|
let cr = (*instance).compile_result2.as_ref().unwrap();
|
||||||
cr
|
cr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe fn get_compile_result<'a>(instance: *const Instance) -> Option<&'a CompileResult> {
|
||||||
|
(*instance).compile_result2.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_num_diagnostics2(instance: *const Instance) -> u32 {
|
unsafe extern "C" fn haku_num_diagnostics(instance: *const Instance) -> u32 {
|
||||||
(*instance).diagnostics2.len() as u32
|
(*instance).diagnostics2.len() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_diagnostic_start2(instance: *const Instance, index: u32) -> u32 {
|
unsafe extern "C" fn haku_diagnostic_start(instance: *const Instance, index: u32) -> u32 {
|
||||||
(*instance).diagnostics2[index as usize].span().start
|
(*instance).diagnostics2[index as usize].span().start
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_diagnostic_end2(instance: *const Instance, index: u32) -> u32 {
|
unsafe extern "C" fn haku_diagnostic_end(instance: *const Instance, index: u32) -> u32 {
|
||||||
(*instance).diagnostics2[index as usize].span().end
|
(*instance).diagnostics2[index as usize].span().end
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_diagnostic_message2(instance: *const Instance, index: u32) -> *const u8 {
|
unsafe extern "C" fn haku_diagnostic_message(instance: *const Instance, index: u32) -> *const u8 {
|
||||||
(*instance).diagnostics2[index as usize].message().as_ptr()
|
(*instance).diagnostics2[index as usize].message().as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_diagnostic_message_len2(instance: *const Instance, index: u32) -> u32 {
|
unsafe extern "C" fn haku_diagnostic_message_len(instance: *const Instance, index: u32) -> u32 {
|
||||||
(*instance).diagnostics2[index as usize].message().len() as u32
|
(*instance).diagnostics2[index as usize].message().len() as u32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_defs_len2(instance: *const Instance) -> usize {
|
unsafe extern "C" fn haku_defs_len(instance: *const Instance) -> usize {
|
||||||
compile_result(instance).defs_string.len()
|
unwrap_compile_result(instance).defs_string.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_defs2(instance: *const Instance) -> *const u8 {
|
unsafe extern "C" fn haku_defs(instance: *const Instance) -> *const u8 {
|
||||||
compile_result(instance).defs_string.as_ptr()
|
unwrap_compile_result(instance).defs_string.as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_tags_len2(instance: *const Instance) -> usize {
|
unsafe extern "C" fn haku_tags_len(instance: *const Instance) -> usize {
|
||||||
compile_result(instance).tags_string.len()
|
unwrap_compile_result(instance).tags_string.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_tags2(instance: *const Instance) -> *const u8 {
|
unsafe extern "C" fn haku_tags(instance: *const Instance) -> *const u8 {
|
||||||
compile_result(instance).tags_string.as_ptr()
|
unwrap_compile_result(instance).tags_string.as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_bytecode_len2(instance: *const Instance) -> usize {
|
unsafe extern "C" fn haku_bytecode_len(instance: *const Instance) -> usize {
|
||||||
compile_result(instance).chunk.bytecode.len()
|
unwrap_compile_result(instance).chunk.bytecode.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_bytecode2(instance: *const Instance) -> *const u8 {
|
unsafe extern "C" fn haku_bytecode(instance: *const Instance) -> *const u8 {
|
||||||
compile_result(instance).chunk.bytecode.as_ptr()
|
unwrap_compile_result(instance).chunk.bytecode.as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_local_count2(instance: *const Instance) -> u8 {
|
unsafe extern "C" fn haku_local_count(instance: *const Instance) -> u8 {
|
||||||
compile_result(instance).closure_spec.local_count
|
unwrap_compile_result(instance).closure_spec.local_count
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn haku_stat_ast_size(instance: *const Instance) -> usize {
|
unsafe extern "C" fn haku_stat_ast_size(instance: *const Instance) -> usize {
|
||||||
compile_result(instance).stats.ast_size
|
get_compile_result(instance)
|
||||||
|
.map(|cr| cr.stats.ast_size)
|
||||||
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,8 +71,6 @@ pub const Defs = struct {
|
||||||
.num_tags = mem.count(u8, tags_string, "\n"),
|
.num_tags = mem.count(u8, tags_string, "\n"),
|
||||||
};
|
};
|
||||||
|
|
||||||
log.debug("parsed defs {*} tags {s}: {}", .{ defs_string, tags_string, d });
|
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -471,8 +471,8 @@ impl SessionLoop {
|
||||||
|
|
||||||
Interaction::Dotter { from, to, num } => {
|
Interaction::Dotter { from, to, num } => {
|
||||||
if brush_ok {
|
if brush_ok {
|
||||||
jumpstart_trampoline2(&mut haku);
|
jumpstart_trampoline(&mut haku);
|
||||||
match haku.cont2() {
|
match haku.cont() {
|
||||||
haku2::Cont::Dotter(dotter) => match dotter.run(&haku2::Dotter {
|
haku2::Cont::Dotter(dotter) => match dotter.run(&haku2::Dotter {
|
||||||
from: (from.x, from.y),
|
from: (from.x, from.y),
|
||||||
to: (to.x, to.y),
|
to: (to.x, to.y),
|
||||||
|
@ -487,9 +487,9 @@ impl SessionLoop {
|
||||||
}
|
}
|
||||||
|
|
||||||
Interaction::Scribble => {
|
Interaction::Scribble => {
|
||||||
match haku.cont2() {
|
match haku.cont() {
|
||||||
haku2::Cont::None => {
|
haku2::Cont::None => {
|
||||||
draw_to_chunks2(&wall, current_render_area, &mut haku);
|
draw_to_chunks(&wall, current_render_area, &mut haku);
|
||||||
}
|
}
|
||||||
_ => error!("tried to draw a scribble with an active continuation"),
|
_ => error!("tried to draw a scribble with an active continuation"),
|
||||||
}
|
}
|
||||||
|
@ -547,16 +547,16 @@ fn chunks_to_modify(
|
||||||
chunks
|
chunks
|
||||||
}
|
}
|
||||||
|
|
||||||
fn jumpstart_trampoline2(haku: &mut Haku) {
|
fn jumpstart_trampoline(haku: &mut Haku) {
|
||||||
if !haku.has_cont2() {
|
if !haku.has_cont() {
|
||||||
if let Err(e) = haku.eval_brush2() {
|
if let Err(e) = haku.eval_brush() {
|
||||||
error!("eval_brush2 exception: {e:?}");
|
error!("eval_brush2 exception: {e:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(wall, vm))]
|
#[instrument(skip(wall, vm))]
|
||||||
fn draw_to_chunks2(wall: &Wall, render_area: RenderArea, vm: &mut Haku) {
|
fn draw_to_chunks(wall: &Wall, render_area: RenderArea, vm: &mut Haku) {
|
||||||
let settings = wall.settings();
|
let settings = wall.settings();
|
||||||
|
|
||||||
let chunk_size = settings.chunk_size as f32;
|
let chunk_size = settings.chunk_size as f32;
|
||||||
|
@ -571,7 +571,7 @@ fn draw_to_chunks2(wall: &Wall, render_area: RenderArea, vm: &mut Haku) {
|
||||||
let chunk_ref = wall.get_or_create_chunk(ChunkPosition::new(chunk_x, chunk_y));
|
let chunk_ref = wall.get_or_create_chunk(ChunkPosition::new(chunk_x, chunk_y));
|
||||||
let mut chunk = chunk_ref.blocking_lock();
|
let mut chunk = chunk_ref.blocking_lock();
|
||||||
let mut canvas = ChunkCanvas::new(&mut chunk).translated(x, y);
|
let mut canvas = ChunkCanvas::new(&mut chunk).translated(x, y);
|
||||||
if let Err(e) = vm.render2(&mut canvas, 256) {
|
if let Err(e) = vm.render(&mut canvas, 256) {
|
||||||
error!(chunk_x, chunk_y, "draw_to_chunks2 exception: {e:?}");
|
error!(chunk_x, chunk_y, "draw_to_chunks2 exception: {e:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ pub struct Haku {
|
||||||
defs: Defs,
|
defs: Defs,
|
||||||
defs_image: DefsImage,
|
defs_image: DefsImage,
|
||||||
|
|
||||||
vm2: Option<haku2::Vm>,
|
vm: Option<haku2::Vm>,
|
||||||
|
|
||||||
brush: Option<(ChunkId, ClosureSpec)>,
|
brush: Option<(ChunkId, ClosureSpec)>,
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ impl Haku {
|
||||||
system_image,
|
system_image,
|
||||||
defs,
|
defs,
|
||||||
defs_image,
|
defs_image,
|
||||||
vm2: None,
|
vm: None,
|
||||||
brush: None,
|
brush: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ impl Haku {
|
||||||
self.brush = Some((chunk_id, closure_spec));
|
self.brush = Some((chunk_id, closure_spec));
|
||||||
|
|
||||||
let scratch = self
|
let scratch = self
|
||||||
.vm2
|
.vm
|
||||||
.take()
|
.take()
|
||||||
.map(|vm| vm.into_scratch())
|
.map(|vm| vm.into_scratch())
|
||||||
.unwrap_or_else(|| haku2::Scratch::new(self.limits.memory));
|
.unwrap_or_else(|| haku2::Scratch::new(self.limits.memory));
|
||||||
|
@ -140,7 +140,7 @@ impl Haku {
|
||||||
stack_capacity: self.limits.stack_capacity,
|
stack_capacity: self.limits.stack_capacity,
|
||||||
call_stack_capacity: self.limits.call_stack_capacity,
|
call_stack_capacity: self.limits.call_stack_capacity,
|
||||||
});
|
});
|
||||||
self.vm2 = Some(haku2::Vm::new(scratch, code, limits));
|
self.vm = Some(haku2::Vm::new(scratch, code, limits));
|
||||||
|
|
||||||
info!("brush set successfully");
|
info!("brush set successfully");
|
||||||
|
|
||||||
|
@ -148,9 +148,9 @@ impl Haku {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), err(level = Level::INFO))]
|
#[instrument(skip(self), err(level = Level::INFO))]
|
||||||
pub fn eval_brush2(&mut self) -> eyre::Result<()> {
|
pub fn eval_brush(&mut self) -> eyre::Result<()> {
|
||||||
let vm = self
|
let vm = self
|
||||||
.vm2
|
.vm
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or_eyre("brush is not compiled and ready to be used")?;
|
.ok_or_eyre("brush is not compiled and ready to be used")?;
|
||||||
vm.begin(self.limits.fuel as u32)
|
vm.begin(self.limits.fuel as u32)
|
||||||
|
@ -159,13 +159,9 @@ impl Haku {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self, canvas, max_depth), err(level = Level::INFO))]
|
#[instrument(skip(self, canvas, max_depth), err(level = Level::INFO))]
|
||||||
pub fn render2(
|
pub fn render(&mut self, canvas: &mut dyn haku2::Canvas, max_depth: usize) -> eyre::Result<()> {
|
||||||
&mut self,
|
|
||||||
canvas: &mut dyn haku2::Canvas,
|
|
||||||
max_depth: usize,
|
|
||||||
) -> eyre::Result<()> {
|
|
||||||
let vm = self
|
let vm = self
|
||||||
.vm2
|
.vm
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or_eyre("VM is not ready for rendering")?;
|
.ok_or_eyre("VM is not ready for rendering")?;
|
||||||
vm.render(canvas, max_depth)
|
vm.render(canvas, max_depth)
|
||||||
|
@ -173,11 +169,11 @@ impl Haku {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_cont2(&mut self) -> bool {
|
pub fn has_cont(&mut self) -> bool {
|
||||||
self.vm2.as_mut().expect("VM is not started").has_cont()
|
self.vm.as_mut().expect("VM is not started").has_cont()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cont2(&mut self) -> haku2::Cont<'_> {
|
pub fn cont(&mut self) -> haku2::Cont<'_> {
|
||||||
self.vm2.as_mut().expect("VM is not started").cont()
|
self.vm.as_mut().expect("VM is not started").cont()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -250,20 +250,27 @@ export class Haku {
|
||||||
setBrush(code) {
|
setBrush(code) {
|
||||||
w.haku_reset(this.#pInstance);
|
w.haku_reset(this.#pInstance);
|
||||||
|
|
||||||
|
if (this.#pVm2 != 0) w2.haku2_vm_destroy(this.#pVm2);
|
||||||
|
if (this.#pDefs2 != 0) w2.haku2_defs_destroy(this.#pDefs2);
|
||||||
|
if (this.#bytecode2 != null) freeString2(this.#bytecode2);
|
||||||
|
this.#pVm2 = 0;
|
||||||
|
this.#pDefs2 = 0;
|
||||||
|
this.#bytecode2 = null;
|
||||||
|
|
||||||
let brushCode = allocString(code);
|
let brushCode = allocString(code);
|
||||||
let statusCode = w.haku_compile_brush2(this.#pInstance, brushCode.length, brushCode.ptr);
|
let statusCode = w.haku_compile_brush(this.#pInstance, brushCode.length, brushCode.ptr);
|
||||||
freeString(brushCode);
|
freeString(brushCode);
|
||||||
if (!w.haku_is_ok(statusCode)) {
|
if (!w.haku_is_ok(statusCode)) {
|
||||||
if (w.haku_is_diagnostics_emitted(statusCode)) {
|
if (w.haku_is_diagnostics_emitted(statusCode)) {
|
||||||
let diagnostics = [];
|
let diagnostics = [];
|
||||||
for (let i = 0; i < w.haku_num_diagnostics2(this.#pInstance); ++i) {
|
for (let i = 0; i < w.haku_num_diagnostics(this.#pInstance); ++i) {
|
||||||
diagnostics.push({
|
diagnostics.push({
|
||||||
start: w.haku_diagnostic_start2(this.#pInstance, i),
|
start: w.haku_diagnostic_start(this.#pInstance, i),
|
||||||
end: w.haku_diagnostic_end2(this.#pInstance, i),
|
end: w.haku_diagnostic_end(this.#pInstance, i),
|
||||||
message: readString(
|
message: readString(
|
||||||
memory,
|
memory,
|
||||||
w.haku_diagnostic_message_len2(this.#pInstance, i),
|
w.haku_diagnostic_message_len(this.#pInstance, i),
|
||||||
w.haku_diagnostic_message2(this.#pInstance, i),
|
w.haku_diagnostic_message(this.#pInstance, i),
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -281,28 +288,22 @@ export class Haku {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.#pVm2 != 0) w2.haku2_vm_destroy(this.#pVm2);
|
|
||||||
if (this.#pDefs2 != 0) w2.haku2_defs_destroy(this.#pDefs2);
|
|
||||||
if (this.#bytecode2 != null) freeString2(this.#bytecode2);
|
|
||||||
|
|
||||||
console.log(w.haku_defs2(this.#pInstance), w.haku_defs_len2(this.#pInstance));
|
|
||||||
|
|
||||||
let pDefsString = dup1to2(
|
let pDefsString = dup1to2(
|
||||||
w.haku_defs2(this.#pInstance),
|
w.haku_defs(this.#pInstance),
|
||||||
w.haku_defs_len2(this.#pInstance),
|
w.haku_defs_len(this.#pInstance),
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
let pTagsString = dup1to2(
|
let pTagsString = dup1to2(
|
||||||
w.haku_tags2(this.#pInstance),
|
w.haku_tags(this.#pInstance),
|
||||||
w.haku_tags_len2(this.#pInstance),
|
w.haku_tags_len(this.#pInstance),
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
this.#pDefs2 = allocCheck(
|
this.#pDefs2 = allocCheck(
|
||||||
w2.haku2_defs_parse(
|
w2.haku2_defs_parse(
|
||||||
pDefsString.ptr,
|
pDefsString.ptr,
|
||||||
w.haku_defs_len2(this.#pInstance),
|
w.haku_defs_len(this.#pInstance),
|
||||||
pTagsString.ptr,
|
pTagsString.ptr,
|
||||||
w.haku_tags_len2(this.#pInstance),
|
w.haku_tags_len(this.#pInstance),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -310,11 +311,11 @@ export class Haku {
|
||||||
this.#pVm2 = allocCheck(w2.haku2_vm_new());
|
this.#pVm2 = allocCheck(w2.haku2_vm_new());
|
||||||
|
|
||||||
this.#bytecode2 = dup1to2(
|
this.#bytecode2 = dup1to2(
|
||||||
w.haku_bytecode2(this.#pInstance),
|
w.haku_bytecode(this.#pInstance),
|
||||||
w.haku_bytecode_len2(this.#pInstance),
|
w.haku_bytecode_len(this.#pInstance),
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
this.#localCount = w.haku_local_count2(this.#pInstance);
|
this.#localCount = w.haku_local_count(this.#pInstance);
|
||||||
|
|
||||||
freeString2(pDefsString);
|
freeString2(pDefsString);
|
||||||
freeString2(pTagsString);
|
freeString2(pTagsString);
|
||||||
|
@ -340,10 +341,14 @@ export class Haku {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get ok() {
|
||||||
|
return this.#pVm2 != 0;
|
||||||
|
}
|
||||||
|
|
||||||
beginBrush() {
|
beginBrush() {
|
||||||
if (this.#pVm2 == 0) {
|
if (!this.ok) {
|
||||||
console.warn("VM instance is not available for drawing");
|
console.debug("VM instance is not available for drawing");
|
||||||
return;
|
return { status: "ok" };
|
||||||
}
|
}
|
||||||
|
|
||||||
w2.haku2_scratch_reset(this.#pScratch2);
|
w2.haku2_scratch_reset(this.#pScratch2);
|
||||||
|
@ -415,10 +420,10 @@ export class Haku {
|
||||||
}
|
}
|
||||||
|
|
||||||
get remainingFuel() {
|
get remainingFuel() {
|
||||||
return w2.haku2_vm_fuel(this.#pVm2);
|
return this.ok ? w2.haku2_vm_fuel(this.#pVm2) : this.#fuel;
|
||||||
}
|
}
|
||||||
|
|
||||||
get usedMemory() {
|
get usedMemory() {
|
||||||
return w2.haku2_scratch_used(this.#pScratch2);
|
return this.ok ? w2.haku2_scratch_used(this.#pScratch2) : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,6 +244,8 @@ function readUrl(urlString) {
|
||||||
setInterval(flushInteractionQueue, updateInterval);
|
setInterval(flushInteractionQueue, updateInterval);
|
||||||
|
|
||||||
canvasRenderer.addEventListener(".interact", async (event) => {
|
canvasRenderer.addEventListener(".interact", async (event) => {
|
||||||
|
if (!currentUser.haku.ok) return;
|
||||||
|
|
||||||
let result = await currentUser.haku.evalBrush(
|
let result = await currentUser.haku.evalBrush(
|
||||||
selfController(interactionQueue, wall, event),
|
selfController(interactionQueue, wall, event),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue