That's my code I will try to write a minimal example without the full webpack toolchain.
My code when I'm trying to read the WASM memory directly from the JS.
<html>
<head>
<script src="./Computation/./wasm.js"></script>
<script src="fr.js?version=1624258342"></script> <!-- Webpack generated JS from TypeScript -->
<!-- some CSS and JS dependencies. Jquery, socket.io and more -->
</head>
<body>
<!-- divs andpage layout stuff -->
</body>
declare let wasm_bindgen: any;
const wasm = wasm_bindgen("./Computation/wasm_bg.wasm");
class WasmGraph extends Graphs.Graph<Point, Data.ArcDTO> {
protected _graphUpdate: boolean;
protected _nodes: Array<any> = [];
public constructor() {
super(null);
this._graphUpdate = true;
}
public clear() {
super.clear();
this._graphUpdate = true;
}
public addVertex(v: Graphs.Vertex<Point>): Graphs.Vertex<Point> {
this._graphUpdate = true;
return super.addVertex(v);
}
public addVertexAt(p: Point): Graphs.Vertex<Point> {
this._graphUpdate = true;
return super.addVertexAt(p);
}
public addArc(
from: Graphs.Vertex<Point>, to: Graphs.Vertex<Point>, distance: number,
dualDirection: boolean = true, edgeData: Data.ArcDTO = null) {
this._graphUpdate = true;
return super.addArc(from, to, distance, dualDirection, edgeData);
}
private async buildArrays() {
let obj = await wasm;
this._vertices.forEach((item, index) => {
let j: Array<any> = [];
item.neighbours.forEach((item) => {
j.push({
id: 0,
distance: item.distance,
position: item.to.position
});
});
let i = {
id: index,
neighbours: j,
position: item.position
};
this._nodes.push(i);
});
let no: Array<any> = [];
let ar: Array<any> = [];
let arl: Array<any> = [];
this._nodes.forEach((item) => {
item.neighbours.forEach((i: any) => {
this._nodes.forEach((j) => {
if (i.position == j.position) {
i.id = j.id;
}
});
});
no.push(ar.length);
item.neighbours.forEach((i: any) => {
ar.push(i.id);
arl.push(i.distance);
});
});
let nodes = new Int32Array(obj.memory.buffer, obj.nodes_ptr(), no.length);
let arcs = new Int32Array(obj.memory.buffer, obj.arcs_ptr(), ar.length);
let arc_length = new Float64Array(obj.memory.buffer, obj.arc_length_ptr(), arl.length);
let new_no: Array<any> = [];
no.forEach((item, index) =>{
if (item == no[index - 1]) {
new_no[index] = -1;
}
else {
new_no[index] = item;
}
});
ar.forEach((item, index) => {
arcs[index] = item;
});
new_no.forEach((item, index) => {
nodes[index] = item;
});
arl.forEach((item, index) => {
arc_length[index] = item;
});
}
public async getWasmPath(from: Graphs.Vertex<Point>, to: Graphs.Vertex<Point>, canUseEdge?: (edge: Graphs.Edge<any, any>) => boolean):
Promise<Graphs.VertexAndEdge<Point, Data.ArcDTO>[]> {
if (this._graphUpdate) {
this.buildArrays();
this._graphUpdate = false;
}
let mod = await wasm;
let result = new Int32Array(mod.memory.buffer, mod.result_ptr(), 4900000);
let start: number, end: number;
this._nodes.forEach((item) => {
if (item.position == from.position) {
if (item.id == 0) {
start = item.id + 1;
}
else {
start = item.id;
}
}
else if (item.position == to.position) {
if (item.id == 0) {
end = item.id + 1;
}
else {
end = item.id;
}
}
});
let length = await mod.get_path(start, end);
let res = [];
let index = 0;
while (index < length) {
res[index] = result[index];
index++;
}
console.log(res, length);
return null; // Because, I'm testing stuff.
}
}
#![feature(total_cmp)] // For the dijkstra part
use console_error_panic_hook;
use wasm_bindgen::prelude::*;
mod dijkstra;
mod utils;
static NODES: [i32; 4900000 as usize] = [-2; 4900000 as usize];
static ARCS: [i32; 4900000 as usize] = [-2; 4900000 as usize];
static ARCLENGTH: [f64; 4900000 as usize] = [-2.0; 4900000 as usize];
static mut RESULT: [i32; 4900000 as usize] = [-2; 4900000 as usize];
#[wasm_bindgen]
pub fn nodes_ptr() -> *const i32 {
NODES.as_ptr()
}
#[wasm_bindgen]
pub fn arcs_ptr() -> *const i32 {
ARCS.as_ptr()
}
#[wasm_bindgen]
pub fn arc_length_ptr() -> *const f64 {
ARCLENGTH.as_ptr()
}
#[wasm_bindgen]
pub fn result_ptr() -> *const i32 {
unsafe { RESULT.as_ptr() }
}
#[wasm_bindgen]
pub fn get_path(from: i32, to: i32) -> usize {
console_error_panic_hook::set_once();
let mut nodes = Vec::new();
for i in &NODES {
if *i == -2 as i32 {
continue;
} else {
nodes.push(*i);
}
}
let mut arcs = Vec::new();
for i in &ARCS {
if *i == -2 as i32 {
continue;
} else {
arcs.push(*i);
}
}
let mut arcs_lengths = Vec::new();
for i in &ARCLENGTH {
if *i == -2 as f64 {
continue;
} else {
arcs_lengths.push(*i);
}
}
log!("{:?}, {:?}", from, to);
let path = match dijkstra::dijkstra(nodes, arcs, arcs_lengths, from, to) {
Ok(res) => res,
Err(e) => {
log!("{}", e);
return 0 as usize;
}
};
log!("{:?}", path.0);
let mut j = 0;
for i in path.0.as_slice() {
unsafe { RESULT[j] = *i }
j += 1;
}
return path.0.len();
}