feat: failed attempt to make emscripten work
This commit is contained in:
parent
bb52b98ae7
commit
110eab7a1a
43 changed files with 700 additions and 148 deletions
2
wasm/config_env_wasm.h
Normal file
2
wasm/config_env_wasm.h
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// the config file for wasm
|
||||
#define PRINT_MODE 1
|
||||
81
wasm/emscripten.c
Normal file
81
wasm/emscripten.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include "emscripten.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
EM_JS(void, log_to_js, (char* buff, size_t len), {
|
||||
log_intake(buff, len);
|
||||
});
|
||||
|
||||
int printf_wrap(const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
char* out;
|
||||
vsprintf(out, format, args);
|
||||
char* sharedPos = (char*) malloc(strlen(out) * sizeof(char));
|
||||
strcpy(sharedPos, out);
|
||||
log_to_js((void*) sharedPos, strlen(out));
|
||||
va_end(args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include "../src/config.h"
|
||||
#include "../src/utils.h"
|
||||
#include "../src/types.h"
|
||||
#include "../src/state.h"
|
||||
#include "../src/line_processing.h"
|
||||
|
||||
struct Result {
|
||||
byte type;
|
||||
int data;
|
||||
};
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void* wasm_init_state()
|
||||
{
|
||||
struct StateContainer* ptr = state_init();
|
||||
return (void*) ptr;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void* wasm_process_line(void* state, char* line)
|
||||
{
|
||||
struct Result* res = malloc(sizeof(struct Result));
|
||||
|
||||
process_line(state, line);
|
||||
|
||||
res->type = ((struct StateContainer*) state)->lastEvaluationType;
|
||||
res->data = ((struct StateContainer*) state)->lastEvaluationResult;
|
||||
|
||||
return (void*) res;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void wasm_process_script(char* scriptLines)
|
||||
{
|
||||
printf_wrap("Will process script \"%s\" \n", scriptLines);
|
||||
struct StateContainer* state = state_init();
|
||||
process_script(state, scriptLines);
|
||||
}
|
||||
|
||||
// very dangerous indeed
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int wasm_get_type_from_ref(void* ref) {
|
||||
return ((struct Result*) ref)->type;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int wasm_get_int_from_ref(void* ref) {
|
||||
return ((struct Result*) ref)->data;
|
||||
}
|
||||
|
||||
// EMSCRIPTEN_KEEPALIVE
|
||||
// int wasm_info(char* lines)
|
||||
// {
|
||||
// struct StateContainer* state = state_init();
|
||||
// process_script(state, lines);
|
||||
|
||||
// return state->lastEvaluationResult;
|
||||
// }
|
||||
|
||||
1
wasm/emscripten_raw.html
Normal file
1
wasm/emscripten_raw.html
Normal file
File diff suppressed because one or more lines are too long
1
wasm/emscripten_raw.js
Normal file
1
wasm/emscripten_raw.js
Normal file
File diff suppressed because one or more lines are too long
BIN
wasm/emscripten_raw.wasm
Executable file
BIN
wasm/emscripten_raw.wasm
Executable file
Binary file not shown.
69
wasm/index.html
Normal file
69
wasm/index.html
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Langatator demo</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
|
||||
<style>
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 90%;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.form {
|
||||
width: 100%;
|
||||
margin-top: 1em;
|
||||
}
|
||||
|
||||
#script-input {
|
||||
width: 100%;
|
||||
min-width: 100%;
|
||||
max-width: 100%;
|
||||
display: block;
|
||||
min-height: 20em;
|
||||
|
||||
}
|
||||
|
||||
.submit-btn {
|
||||
margin-top: 1em;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Langatator demo</h1>
|
||||
<a href="https://gitlab.com/lefuturiste/langatator">The git repository</a>
|
||||
<div class="form">
|
||||
<textarea id="script-input"></textarea>
|
||||
<button
|
||||
class="submit-btn"
|
||||
onclick="submitScript()"
|
||||
>
|
||||
Process script!
|
||||
</button>
|
||||
</div>
|
||||
<div class="console">
|
||||
<pre id="console-output">
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// const oldLog = console.log;
|
||||
// console.log = function (message) {
|
||||
// oldLog.apply(console, ['captured', ...arguments]);
|
||||
// };
|
||||
</script>
|
||||
<script src="./langatator_adapter.js"></script>
|
||||
<script src="./script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
1
wasm/langatator_adapter.js
Normal file
1
wasm/langatator_adapter.js
Normal file
File diff suppressed because one or more lines are too long
BIN
wasm/langatator_adapter.wasm
Executable file
BIN
wasm/langatator_adapter.wasm
Executable file
Binary file not shown.
93
wasm/script.js
Normal file
93
wasm/script.js
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
'use strict';
|
||||
|
||||
// let app = document.getElementById("app");
|
||||
// let wasm = null;
|
||||
// let iota = 0;
|
||||
|
||||
// function cstrlen(mem, ptr) {
|
||||
// let len = 0;
|
||||
// while (mem[ptr] != 0) {
|
||||
// len++;
|
||||
// ptr++;
|
||||
// }
|
||||
// return len;
|
||||
// }
|
||||
|
||||
// function cstr_by_ptr(mem_buffer, ptr) {
|
||||
// const mem = new Uint8Array(mem_buffer);
|
||||
// const len = cstrlen(mem, ptr);
|
||||
// const bytes = new Uint8Array(mem_buffer, ptr, len);
|
||||
// return new TextDecoder().decode(bytes);
|
||||
// }
|
||||
|
||||
// function platform_panic(file_path_ptr, line, message_ptr) {
|
||||
// const buffer = wasm.instance.exports.memory.buffer;
|
||||
// const file_path = cstr_by_ptr(buffer, file_path_ptr);
|
||||
// const message = cstr_by_ptr(buffer, message_ptr);
|
||||
// console.error(file_path+":"+line+": "+message);
|
||||
// }
|
||||
|
||||
// function platform_log(message_ptr) {
|
||||
// const buffer = wasm.instance.exports.memory.buffer;
|
||||
// const message = cstr_by_ptr(buffer, message_ptr);
|
||||
// console.log(message);
|
||||
// }
|
||||
|
||||
// WebAssembly.instantiateStreaming(fetch('langatator.wasm'), {
|
||||
// env: {
|
||||
// platform_panic,
|
||||
// platform_log
|
||||
// }
|
||||
// }).then((w) => {
|
||||
// wasm = w;
|
||||
|
||||
// console.log(wasm.instance.exports.wasm_info());
|
||||
|
||||
// //const buffer = wasm.instance.exports.memory.buffer;
|
||||
// });
|
||||
|
||||
const consoleOutput = document.getElementById('console-output')
|
||||
|
||||
function log_intake(ptr, len) {
|
||||
let line = ""
|
||||
for (var i = 0; i < len; i++) {
|
||||
let val = Module.getValue(ptr+i, 'i8')
|
||||
line = line + String.fromCharCode(val)
|
||||
}
|
||||
console.log('Got line', line)
|
||||
|
||||
// const pre= document.createElement('pre')
|
||||
// consoleOutput.appendChild(pre)
|
||||
}
|
||||
|
||||
|
||||
Module.onRuntimeInitialized = async _ => {
|
||||
const api = {
|
||||
init: Module.cwrap('wasm_init_state', 'number', []),
|
||||
processLine: Module.cwrap('wasm_process_line', 'number', ['number', 'string']),
|
||||
getTypeFromRef: Module.cwrap('wasm_get_type_from_ref', 'number', ['number']),
|
||||
getIntFromRef: Module.cwrap('wasm_get_int_from_ref', 'number', ['number']),
|
||||
|
||||
// standalone (integrated state initialization)
|
||||
processScript: Module.cwrap('wasm_process_script', 'number', ['string']),
|
||||
};
|
||||
console.log(api);
|
||||
|
||||
// const ref = api.init();
|
||||
// console.log(`Got ref: ${ref}`);
|
||||
|
||||
window.processLine = (line) => {
|
||||
const resultRef = api.processLine(ref, line);
|
||||
console.log(`Got result ref: ${resultRef}`);
|
||||
console.log(api.getTypeFromRef(resultRef));
|
||||
console.log(api.getIntFromRef(resultRef));
|
||||
}
|
||||
|
||||
window.submitScript = () => {
|
||||
const textarea = document.getElementById('script-input')
|
||||
api.processScript(textarea.value)
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
0
wasm/style.css
Normal file
0
wasm/style.css
Normal file
44
wasm/wasm.c
Normal file
44
wasm/wasm.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdlib.h>
|
||||
#include "../src/config.h"
|
||||
#define CONFIG_PRINT_METHOD PRINT_METHOD_WASM
|
||||
#include "../src/utils.h"
|
||||
#include "../src/types.h"
|
||||
#include "../src/state.h"
|
||||
#include "../src/line_processing.h"
|
||||
|
||||
void* wasm_init_state()
|
||||
{
|
||||
struct StateContainer* ptr = state_init();
|
||||
return (void*) ptr;
|
||||
}
|
||||
|
||||
void* wasm_process_line(void* state, char* line)
|
||||
{
|
||||
struct Result* res = malloc(sizeof(struct Result));
|
||||
|
||||
process_line(state, line);
|
||||
|
||||
res->type = ((struct StateContainer*) state)->lastEvaluationType;
|
||||
res->data = ((struct StateContainer*) state)->lastEvaluationResult;
|
||||
|
||||
return (void*) res;
|
||||
}
|
||||
|
||||
// very dangerous indeed
|
||||
int wasm_get_type_from_ref(void* ref) {
|
||||
return ((struct Result*) ref)->type;
|
||||
}
|
||||
|
||||
int wasm_get_int_from_ref(void* ref) {
|
||||
return ((struct Result*) ref)->data;
|
||||
}
|
||||
|
||||
// EMSCRIPTEN_KEEPALIVE
|
||||
// int wasm_info(char* lines)
|
||||
// {
|
||||
// struct StateContainer* state = state_init();
|
||||
// process_script(state, lines);
|
||||
|
||||
// return state->lastEvaluationResult;
|
||||
// }
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue