Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

fix: Add or cleanup implementations for JS target #199

Merged
merged 1 commit into from
May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ mod native {

#[cfg(not(feature = "native"))]
mod wasm {
use std::cell::Cell;
use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store, Value};

use super::{Barretenberg, Error, FeatureError};
Expand Down Expand Up @@ -254,11 +253,10 @@ mod wasm {

#[cfg(feature = "js")]
{
let view: js_sys::Uint8Array = memory.uint8view();
let view = memory.uint8view();
for (byte_id, cell_id) in (offset..(offset + arr.len())).enumerate() {
view.set_index(cell_id as u32, arr[byte_id])
}
return;
}

#[cfg(not(feature = "js"))]
Expand All @@ -284,12 +282,15 @@ mod wasm {
let end = start + length;

#[cfg(feature = "js")]
return memory.uint8view().to_vec()[start..end].to_vec();
return memory
.uint8view()
.subarray(start as u32, end as u32)
.to_vec();

#[cfg(not(feature = "js"))]
return memory.view()[start..end]
.iter()
.map(|cell: &Cell<u8>| cell.get())
.map(|cell| cell.get())
.collect();
}

Expand Down Expand Up @@ -405,14 +406,29 @@ mod wasm {
let mut ptr_end = 0;
let byte_view = env.memory.uint8view();

#[cfg(feature = "js")]
for (i, cell) in byte_view.to_vec()[ptr as usize..].iter().enumerate() {
if cell != &0_u8 {
ptr_end = i;
} else {
break;
}
}

#[cfg(not(feature = "js"))]
for (i, cell) in byte_view[ptr as usize..].iter().enumerate() {
if cell != &Cell::new(0) {
if cell.get() != 0 {
ptr_end = i;
} else {
break;
}
}

#[cfg(feature = "js")]
let str_vec: Vec<_> =
byte_view.to_vec()[ptr as usize..=(ptr + ptr_end as i32) as usize].to_vec();
phated marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(not(feature = "js"))]
let str_vec: Vec<_> = byte_view[ptr as usize..=(ptr + ptr_end as i32) as usize]
.iter()
.cloned()
Expand Down