Skip to content

Commit

Permalink
count bytes in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 2, 2023
1 parent 2d16e1b commit d2dfe40
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 59 deletions.
44 changes: 29 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,28 @@
border-radius: 4px;
margin-top: 15px;
font-size: 14px;
white-space: pre-wrap; /* Since CSS 2.1 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
white-space: pre-wrap; /* Since CSS 2.1 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
#stats {
font-family: "Iosevka Web";
margin-top: 10px;
color: #333;
}
</style>
</head>
<body>
<form id="wasmForm">
<textarea id="inputVal" placeholder="Write rust code here">
pub fn eval(&mut self, s: String) {
let st = &mut vec![];
let b = &mut 0;
let a = &mut self.a;
let p = &mut self.p;
let ip = &mut self.ip;
let mut st = &mut vec![];
let mut b = &mut 0;
let mut a = &mut self.a;
let mut p = &mut self.p;
let mut ip = &mut self.ip;
let by = s.as_bytes();
while *ip < by.len() {
let c = by[*ip];
Expand Down Expand Up @@ -96,6 +101,7 @@
</textarea>
<button type="submit">Enter</button>
<pre><div id="output"></div></pre>
<div id="stats"></div>
</form>

<script type="module">
Expand All @@ -104,11 +110,19 @@
init();

document.getElementById('wasmForm').addEventListener('submit', function(e) {
e.preventDefault();
const inputValue = document.getElementById('inputVal').value;
const result = mkterse(inputValue);
document.getElementById('output').textContent = result;
});
e.preventDefault();
const inputValue = document.getElementById('inputVal').value;
const result = mkterse(inputValue);
document.getElementById('output').textContent = result;

const inputBytes = new TextEncoder().encode(inputValue).length;
const outputBytes = new TextEncoder().encode(result).length;

document.getElementById('stats').innerHTML = `
Input: ${inputValue.length} chars, ${inputBytes} bytes<br>
Output: ${result.length} chars, ${outputBytes} bytes
`;
});
</script>
</body>
</html>
Expand Down
6 changes: 0 additions & 6 deletions pkg/libfranca.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
* @returns {string}
*/
export function mkterse(i: string): string;
/**
* @param {string} src
* @returns {string}
*/
export function tersify(src: string): string;

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly mkterse: (a: number, b: number, c: number) => void;
readonly tersify: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
Expand Down
23 changes: 0 additions & 23 deletions pkg/libfranca.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,6 @@ export function mkterse(i) {
}
}

/**
* @param {string} src
* @returns {string}
*/
export function tersify(src) {
let deferred2_0;
let deferred2_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.tersify(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred2_0 = r0;
deferred2_1 = r1;
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
}
}

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
Expand Down
Binary file modified pkg/libfranca_bg.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion pkg/libfranca_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function mkterse(a: number, b: number, c: number): void;
export function tersify(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
Expand Down
14 changes: 0 additions & 14 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
pub mod transformer;

use quote::quote;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub fn tersify(src: String) -> String {
let t1 = transformer::mkterse(src);
// let t2 = conds::terser_conds(t1);
let gen = quote! { #t1 };
println!("{}", gen);
let f: syn::File = syn::parse2(gen).unwrap();
let pretty = prettyplease::unparse(&f);
pretty.to_string()
}

/// forks
#[macro_export]
macro_rules! atop {
Expand Down

0 comments on commit d2dfe40

Please sign in to comment.