Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C3 WASM #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions old/content/Basics/5.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: "For"
title: "For Loop"
weight: 5
---
{{<start>}}
- standard `for` loop syntax
- same syntax as: C and JavaScript
{{<end5>}}

3 changes: 2 additions & 1 deletion old/content/Basics/7.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ title: "If"
weight: 7
---
{{<start>}}
- standard `if` syntax
- syntax same as: C and JavaScript
{{<end7>}}

7 changes: 4 additions & 3 deletions old/content/Basics/8.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
title: "Switch"
title: "Switch Case"
weight: 8
---
{{<start>}}
- `case` statements automatically break.
- C3 switch/case syntax
- C3 `case` statements automatically break.
- note that `case` statements in C and JavaScript do not automatically break
{{<end8>}}

3 changes: 1 addition & 2 deletions old/content/Basics/9.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
title: "Nextcase"
title: "Switch Nextcase"
weight: 9
---
{{<start>}}
- Use `nextcase` to fallthrough to the next statement.
- Empty case statements have implicit fallthrough.
{{<end9>}}

15 changes: 15 additions & 0 deletions old/content/Basics/wasm1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "WASM @extern"
---
- @wasm
- @extern("NAME")
- A function that is to be called from JavaScript should be marked with @wasm and @extern
- From JavaScript these functions are exposed as: wasm.instance.exports.NAME
```
fn void onclick( int x, int y ) @extern("onclick") @wasm {
js_eval(`
window.alert("hello click")
`);
}
```
- Above, the function is exported to JavaScript and callable as: `wasm.instance.exports.onclick(x,y)`
22 changes: 22 additions & 0 deletions old/content/Basics/wasm2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "WASM external functions"
---
- External functions are provided by the runtime environment or the linker.
- When C3 compiles for the WASM target the runtime environment is a JavaScript object.
- In the example below `js_eval` is declared as an external function that is provided by the JavaScript object.
```
extern fn int js_eval(char*ptr);

fn void onclick( int x, int y ) @extern("onclick") @wasm {
js_eval(`
window.alert("hello click")
`);
}
```
- In JavaScript `js_eval` is defined as:
```
js_eval(a){
return eval(cstr_by_ptr(this.wasm.instance.exports.memory.buffer,a))
}

```
57 changes: 57 additions & 0 deletions old/content/Basics/wasm3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "WASM JavaScript Bridge"
---
- WASM is loaded by JavaScript using: `WebAssembly.instantiate`.
- The first argument to `WebAssembly.instantiate` is the wasm file (ArrayBuffer of bytes)
- The second argument to `WebAssembly.instantiate` is an Object with `env`.
```
extern fn int js_eval(char*ptr);

fn void onclick( int x, int y ) @extern("onclick") @wasm {
js_eval(`
window.alert("hello click")
`);
}
```
- The above C3 code calls `js_eval` passing a string to eval in JavaScript.
- Below is an example JavaScript bridge (`env`).
```
class myapi{
reset(wasm){
this.wasm=wasm;
}
get_env(){
return make_environment(this)
}
js_eval(a){
return eval(cstr_by_ptr(this.wasm.instance.exports.memory.buffer,a))
}
onclick(e){
console.log("onclick:", e);
this.wasm.instance.exports.onclick(e.x,e.y)
}
}
```
- Above requires these JavaScript helper functions
```
function make_environment(e){
return new Proxy(e,{
get(t,p,r) {
if(e[p]!==undefined){return e[p].bind(e)}
return(...args)=>{throw p}
}
});
}

function cstrlen(m,p){
var l=0;
while(m[p]!=0){l++;p++}
return l;
}

function cstr_by_ptr(m,p){
const l=cstrlen(new Uint8Array(m),p);
const b=new Uint8Array(m,p,l);
return new TextDecoder().decode(b)
}
```
15 changes: 15 additions & 0 deletions old/content/More/syntax-error1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "Error: Expected ';'"
---
- A statement in C3 ends with a semicolon `;`
- Below: a simple function call statement, the line ends with `;`
```
some_function_call();
```
- Note that Structs and Unions are not terminated with `;`.
- Below is invalid and will throw: `Error: ';' wasn't expected here, try removing it.`
```
struct MyStruct{
int a;
};
```
14 changes: 14 additions & 0 deletions old/content/More/syntax-error2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Error: Expected the ending ')' here."
---
- A function call in C3 begins with the function name,
- followed by the function arguments in parenthesis.
- FUNCTION_NAME( OPTIONAL_ARGUMENTS,... )
```
some_function_call( a, b, c);
```
- Below, is missing the closing parenthesis, and throws: `Error: Expected an ending ')'. Did you forget a ')' before this ';'?`
```
some_function_call(a,b,c;
```

13 changes: 13 additions & 0 deletions old/content/More/syntax-error3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: "Error: A type name was expected"
---
- C3 functions are defined as: `fn RETURN_TYPE NAME ( ARGS...)`,
- Just like in C the return type of the function comes before the function name.
```
fn void myfunc() {}
```
- Below, is invalid, the return type is missing:
```
fn myfunc() {}
```
- Above will throw this error: `Error: A type name was expected, but this looks a variable or function name (as it doesn't start with an uppercase letter).`