Skip to content

Commit

Permalink
$canvas.width ok in demo
Browse files Browse the repository at this point in the history
smart node
  • Loading branch information
pannous committed Nov 26, 2024
1 parent e6afc30 commit 9603c20
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 28 deletions.
4 changes: 4 additions & 0 deletions docs/assets/ln_does_not_persist_need_to_cp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cp ../../cmake-build-wasm-hosted/wasp-hosted.wasm .
cp ../../cmake-build-wasm-debug/wasp.wasm wasp-debug.wasm
cp ../../cmake-build-wasm-runtime/wasp-runtime.wasm .
cp ../../cmake-build-wasm-release/wasp.wasm .
Binary file modified docs/assets/wasp-debug.wasm
Binary file not shown.
Binary file modified docs/assets/wasp-hosted.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/examples.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let exampleCode = {
getElementById: '$canvas.width', //results.value',
getElementById: '$canvas.width', //results.value=1',
hello: `"Hello, World!" // last item in root block is its return value, which is printed"`,
math2: `√π²`,
function: `square := it*it; square 3`,
Expand Down
38 changes: 32 additions & 6 deletions docs/wasp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let wasm_pointer_size = 4;// 32 bit todo: 64 bit on demand
let string_header_32 = 0x10000000
let array_header_32 = 0x40000000
let node_header_32 = 0x80000000
let node_header_64 = 0x0a000000 // first part of 64 bit pointer

function binary_hack(binary_as_text) {
let binary = new Uint8Array(binary_as_text.length);
Expand Down Expand Up @@ -252,6 +253,26 @@ let imports = {
getWasmFunclet,
init_graphics: nop, // canvas init by default
requestAnimationFrame: nop, // todo
getenv: x => {
console.log("getenv", x);
return 0
}, // todo
fopen: x => {
console.log("fopen", x);
return 0
}, // todo WASI / NOT
fprintf: (x, y) => {
console.log("fprintf", x, y);
return 0
}, // todo WASI / NOT
fgetc: x => {
console.log("fgetc", x);
return 0
}, // todo WASI / NOT
fclose: x => {
console.log("fclose", x);
return 0
}, // todo WASI / NOT
getDocumentBody: () => document.body,
createHtml,
addScript: (scriptContent) => {
Expand Down Expand Up @@ -546,6 +567,8 @@ class node {
this.memory = mem // may be lost, or does JS GC keep it?
this.pointer = pointer
if (!pointer) return;//throw "avoid 0 pointer node constructor"
// if(read_int32(pointer, mem)!=node_header_32 && read_int32(pointer, mem)!=0) // todo: WTH??
// pointer = read_int32(pointer, mem)
check_eq(read_int32(pointer, mem), node_header_32, "read_int32(pointer, mem)==node_header_32 @pointer:" + pointer)
pointer += 4;
this.length = read_int32(pointer, mem);
Expand Down Expand Up @@ -705,17 +728,20 @@ function read_array(data, mem) {
return array
}

function smartNode(data0, type /*int32*/, memory) {
function smartNode(data0, type /*int32*/, mem) {
// console.log("smartNode")
type = data0 >> BigInt(32) // shift 32 bits ==
let data = Number(BigInt.asIntN(32, data0))// drop high bits
if (type == string_header_32 || type == string_header_32 >> 8)
return load_chars(data, length = -1, memory, format = 'utf8')
return load_chars(data, length = -1, mem, format = 'utf8')
if (type == array_header_32 || type == array_header_32 >> 8)
return read_array(data, memory)
if (type == node_header_32 || type == node_header_32 >> 8)
return new node(data, memory)
let nod = new node(runtime_exports.smartNode(data0, memory));
return read_array(data, mem)
// if (type == node_header_32 || type == node_header_32 >> 8)
// return new node(data, memory)
// if (type == node_header_64)
// return new node(data, memory)
// runtime_exports.smartNode(data0, memory)) can't access app's memory!
let nod = new node(data, mem);
if (nod.kind == kinds.real || nod.kind == kinds.bool || nod.kind == kinds.long || nod.kind == kinds.codepoint)
return nod.Value() // primitives independent of memory
// if (nod.kind == kinds.object)
Expand Down
13 changes: 11 additions & 2 deletions source/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,18 @@ int ord(Node &p) {

// todo: make constructor
extern "C" Node *smartNode(smart_pointer_64 smartPointer64) {
printf("smartNode(smartPointer64 : %llx\n", (int64) smartPointer64);
printf("smartNode(smartPointer64 : %llx\n", (uint64) smartPointer64);
if (smartPointer64 == 0)return &False;//const_cast<Node *>(&NIL);
if (smartPointer64 == 0x8000000000000000)return new Node(0);
if ((smartPointer64 & node_header_64) == node_header_64) {
Node *node = (Node *) (smartPointer64 & 0x0000FFFFFFFFFFFF);
printf("smartNode: node : %p\n", (void *) node);
printf("smartNode: node : %llu\n", (uint64) (void *) node);
printf("smartNode: node->kind : %d\n", (int) node->kind);
printf("smartNode: node->kind : %s\n", kindName(node->kind));
print("smartNode: node->name : %s\n"s + node->serialize());
return node;
}
// if (!isSmartPointer(smartPointer64))
// return Node(smartPointer64);
if ((smartPointer64 & negative_mask_64) == negative_mask_64) {
Expand Down Expand Up @@ -1419,7 +1428,7 @@ extern "C" Node *smartNode(smart_pointer_64 smartPointer64) {
}

breakpoint_helper
printf("smartPointer64 : %llx\n", (int64) smartPointer64);
printf("smartPointer64 : %llx\n", (uint64) smartPointer64);
error1("missing smart pointer type %x "s % smart_type64 + "" + typeName(Type(smart_type64)) + "");
return new Node();
}
Expand Down
8 changes: 6 additions & 2 deletions source/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef String grapheme;// sequence of one or more code points that are displaye

void newline();

String &hex(int64 d, bool include_0x = true, bool upper_case = false);
String &hex(uint64 d, bool include_0x = true, bool upper_case = false);

enum sizeMeasure {
by_char8s,// bytes
Expand Down Expand Up @@ -683,8 +683,10 @@ class String {
return this->replace("%lld", formatLong(d));
else if (contains("%ld"))
return this->replace("%ld", formatLong(d));
else if (contains("%llu"))
return this->replace("%llu", hex(d));// always unsigned
else if (contains("%llx"))
return this->replace("%llx", hex(d));
return this->replace("%llx", hex(d));// always unsigned
else if (contains("%lx"))
return this->replace("%lx", hex(d));
else if (contains("%l"))
Expand All @@ -693,6 +695,8 @@ class String {
return this->replace("%d", formatLong(d));
else if (contains("%x"))
return this->replace("%x", hex(d));
else if (contains("%p"))// pointer
return this->replace("%p", hex(d));
print("FORMAT:");
printf("%s", data);
printf("int64 arg:");
Expand Down
8 changes: 4 additions & 4 deletions source/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ String load(String file) {
static String zerox = "0x00";
static String ZERO = "0";

String &hex(int64 d0, bool include_0x, bool upper_case) {
int64 d = abs(d0);
String &hex(uint64 d, bool include_0x, bool upper_case) {
// int64 d = abs(d0);
if (d == 0)
return include_0x ? zerox : ZERO;
int size = 4 + 64 / 4;
Expand All @@ -487,12 +487,12 @@ String &hex(int64 d0, bool include_0x, bool upper_case) {
d /= 16;
}
if (include_0x) {
if (abs(d0) < 16)
if (d < 16)
s[i++] = '0';// pad 0x01
s[i++] = 'x';
s[i++] = '0';
}
if (d0 < 0)s[i++] = '-';
if (d < 0)s[i++] = '-'; // no negative hex!
s[i] = 0;
reverseInPlace(s, i);
return *new String(s);
Expand Down
2 changes: 2 additions & 0 deletions source/smart_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Created by me on 15.11.20.
//
typedef long long int64;
typedef unsigned long long uint64;

//typedef double float64;
//typedef int1_t bit / bool 0/1 (flag entry)
//typedef uint2_t bibi™ ;) 0…3
Expand Down
4 changes: 2 additions & 2 deletions source/test_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ void testWasmMemoryIntegrity() {
if (!MAX_MEM) {
error("NO MEMORY");
}
printf("MEMORY start at %lld\n", (int64) memory);
printf("current start at %lld\n", (int64) heap_end);
printf("MEMORY start at %lld\n", (uint64) memory);
printf("current start at %lld\n", (uint64) heap_end);
// Bus error: 10 if i > MEMORY_SIZE
// Fails at 100000, works at 100001 WHERE IS THIS SET?
// int start=125608;
Expand Down
6 changes: 4 additions & 2 deletions source/wasm_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ void printf(char const *format, size_t i);

void printf(char const *format, uint32_t i);

void printf(char const *format, int64 l);
//void printf(char const *format, int64 lld);

void printf(char const *format, int64 l);
void printf(char const *format, uint64 llx);

void printf(char const *format, void* p);

void printf(char const *format, double d);

Expand Down
14 changes: 11 additions & 3 deletions source/wasm_helpers_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ void printf(char const *format, int64 l) {
print(String(format) % l);
}

void printf(char const *format, uint64 l) {
print(String(format) % l);
}

void printf(char const *format, void *l) {
print(String(format) % (uint64) l);
}

void printf(chars format, chars value) {
print(String(format).replace("%s", value));
}
Expand Down Expand Up @@ -111,9 +119,9 @@ void printf(chars format, chars val, int value) {
print(String(format).format((char *) val).format(value));
}

void printf(chars format, void *value) {
print(String(format).replace("%p", String((int64) value)));
}
//void printf(chars format, void *value) {
// print(String(format).replace("%p", String((int64) value)));
//}

extern "C" void *__cxa_allocate_exception(size_t thrown_size) { return 0; }
extern "C" void __cxa_throw(
Expand Down
1 change: 1 addition & 0 deletions source/wasm_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
typedef unsigned char *bytes;
typedef const char *chars;
typedef long long int64;
typedef unsigned long long uint64;
//extern "C" int64 run_wasm(bytes buffer, int buf_size);
extern "C" int64 run_wasm_file(chars wasm_path = "test.wasm");

Expand Down
38 changes: 32 additions & 6 deletions source/wasp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let wasm_pointer_size = 4;// 32 bit todo: 64 bit on demand
let string_header_32 = 0x10000000
let array_header_32 = 0x40000000
let node_header_32 = 0x80000000
let node_header_64 = 0x0a000000 // first part of 64 bit pointer

function binary_hack(binary_as_text) {
let binary = new Uint8Array(binary_as_text.length);
Expand Down Expand Up @@ -252,6 +253,26 @@ let imports = {
getWasmFunclet,
init_graphics: nop, // canvas init by default
requestAnimationFrame: nop, // todo
getenv: x => {
console.log("getenv", x);
return 0
}, // todo
fopen: x => {
console.log("fopen", x);
return 0
}, // todo WASI / NOT
fprintf: (x, y) => {
console.log("fprintf", x, y);
return 0
}, // todo WASI / NOT
fgetc: x => {
console.log("fgetc", x);
return 0
}, // todo WASI / NOT
fclose: x => {
console.log("fclose", x);
return 0
}, // todo WASI / NOT
getDocumentBody: () => document.body,
createHtml,
addScript: (scriptContent) => {
Expand Down Expand Up @@ -546,6 +567,8 @@ class node {
this.memory = mem // may be lost, or does JS GC keep it?
this.pointer = pointer
if (!pointer) return;//throw "avoid 0 pointer node constructor"
// if(read_int32(pointer, mem)!=node_header_32 && read_int32(pointer, mem)!=0) // todo: WTH??
// pointer = read_int32(pointer, mem)
check_eq(read_int32(pointer, mem), node_header_32, "read_int32(pointer, mem)==node_header_32 @pointer:" + pointer)
pointer += 4;
this.length = read_int32(pointer, mem);
Expand Down Expand Up @@ -705,17 +728,20 @@ function read_array(data, mem) {
return array
}

function smartNode(data0, type /*int32*/, memory) {
function smartNode(data0, type /*int32*/, mem) {
// console.log("smartNode")
type = data0 >> BigInt(32) // shift 32 bits ==
let data = Number(BigInt.asIntN(32, data0))// drop high bits
if (type == string_header_32 || type == string_header_32 >> 8)
return load_chars(data, length = -1, memory, format = 'utf8')
return load_chars(data, length = -1, mem, format = 'utf8')
if (type == array_header_32 || type == array_header_32 >> 8)
return read_array(data, memory)
if (type == node_header_32 || type == node_header_32 >> 8)
return new node(data, memory)
let nod = new node(runtime_exports.smartNode(data0, memory));
return read_array(data, mem)
// if (type == node_header_32 || type == node_header_32 >> 8)
// return new node(data, memory)
// if (type == node_header_64)
// return new node(data, memory)
// runtime_exports.smartNode(data0, memory)) can't access app's memory!
let nod = new node(data, mem);
if (nod.kind == kinds.real || nod.kind == kinds.bool || nod.kind == kinds.long || nod.kind == kinds.codepoint)
return nod.Value() // primitives independent of memory
// if (nod.kind == kinds.object)
Expand Down

0 comments on commit 9603c20

Please sign in to comment.