Skip to content

Add Snake testcase #156

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

Merged
merged 14 commits into from
Apr 13, 2023
Merged
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
8 changes: 3 additions & 5 deletions .github/scripts/wasm-check.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

# Can't compare *.wasm file as binary compilation are different
# Can't compare js., order of generated function is different.

cd crate

Expand All @@ -13,6 +14,8 @@ function cmpFile() {
cmp -b $wasm_dir_compare$fileName $wasm_dir$fileName
if [[ $? -ne 0 ]]; then
echo "Please recompile WASM with $fileName"
echo "---content---"
cat $wasm_dir_compare$fileName
exit 1
fi
}
Expand All @@ -21,8 +24,3 @@ for file in ./$wasm_dir_compare/*.ts ; do
[ -L "${file%/}" ] && continue
cmpFile $file
done

for file in ./$wasm_dir_compare/*.js ; do
[ -L "${file%/}" ] && continue
cmpFile $file
done
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ git add backstopjs.approve
git push
```

_Note:_ Add create PAT, personal profile -> Developer Settings -> Fine Grain Token -> Actions(R)/Commit Statues(RW)/Contents(RW)/Metadata(R)

# Vercel/NextJS

1. NextJS on vercel re-uses .next build cache. This sometimes creates an issue, e.g. a page that was once deployed as AMP will forever be recognized as AMP until the cache is cleared.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion backstopjs.approve
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180
231
48 changes: 46 additions & 2 deletions crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ license = "Mozilla Public License Version 2.0"

[dependencies]
wasm-bindgen = "0.2.63"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"

[lib]
crate-type = ["cdylib","rlib"]
Expand Down
11 changes: 6 additions & 5 deletions crate/pkg/snake.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export class World {
* @param {number} width
* @param {number} snake_pos
* @param {number} snake_size
* @param {number} reward_idx
* @returns {World}
*/
static new(width: number, snake_pos: number, snake_size: number): World;
static new(width: number, snake_pos: number, snake_size: number, reward_idx: number): World;
/**
* @returns {number}
*/
Expand All @@ -43,9 +44,9 @@ export class World {
*/
snake_head_idx(): number;
/**
* @returns {number}
* @returns {any}
*/
snake_cells(): number;
snake_cells(): any;
/**
* @returns {number}
*/
Expand All @@ -71,12 +72,12 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_world_free: (a: number) => void;
readonly world_new: (a: number, b: number, c: number) => number;
readonly world_new: (a: number, b: number, c: number, d: number) => number;
readonly world_points: (a: number) => number;
readonly world_reward_cell: (a: number, b: number) => void;
readonly world_width: (a: number) => number;
readonly world_snake_head_idx: (a: number) => number;
readonly world_snake_cells: (a: number) => number;
readonly world_snake_cells: (a: number, b: number) => void;
readonly world_snake_body_length: (a: number) => number;
readonly world_update_direction: (a: number, b: number) => void;
readonly world_play: (a: number) => void;
Expand Down
73 changes: 68 additions & 5 deletions crate/pkg/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ import { rnd } from './snippets/snake-dabe990688d05d0c/src/util/random.js';

let wasm;

const heap = new Array(128).fill(undefined);

heap.push(undefined, null, true, false);

function getObject(idx) { return heap[idx]; }

let heap_next = heap.length;

function dropObject(idx) {
if (idx < 132) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();
Expand Down Expand Up @@ -59,10 +88,11 @@ export class World {
* @param {number} width
* @param {number} snake_pos
* @param {number} snake_size
* @param {number} reward_idx
* @returns {World}
*/
static new(width, snake_pos, snake_size) {
const ret = wasm.world_new(width, snake_pos, snake_size);
static new(width, snake_pos, snake_size, reward_idx) {
const ret = wasm.world_new(width, snake_pos, snake_size, reward_idx);
return World.__wrap(ret);
}
/**
Expand Down Expand Up @@ -101,11 +131,22 @@ export class World {
return ret >>> 0;
}
/**
* @returns {number}
* @returns {any}
*/
snake_cells() {
const ret = wasm.world_snake_cells(this.ptr);
return ret;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.world_snake_cells(retptr, this.ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {number}
Expand Down Expand Up @@ -173,10 +214,32 @@ async function load(module, imports) {
function getImports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbg_rnd_980494400e562b2a = function(arg0) {
const ret = rnd(arg0 >>> 0);
return ret;
};
imports.wbg.__wbindgen_number_new = function(arg0) {
const ret = arg0;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_from_u64 = function(arg0) {
const ret = BigInt.asUintN(64, arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_new_b525de17f44a8943 = function() {
const ret = new Array();
return addHeapObject(ret);
};
imports.wbg.__wbg_set_17224bc548dd1d7b = function(arg0, arg1, arg2) {
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
};
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
Expand Down
73 changes: 2 additions & 71 deletions crate/pkg/snake_bg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rnd } from './snippets/snake-776ed4eb60c4951a/src/util/random.ts';
import { rnd } from './snippets/snake-dabe990688d05d0c/src/util/random.js';

let wasm;
export function __wbg_set_wasm(val) {
Expand All @@ -25,71 +25,6 @@ function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;

let cachedTextEncoder = new lTextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});

function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length);
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len);

const mem = getUint8Memory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}

if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3);
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
}

WASM_VECTOR_LEN = offset;
return ptr;
}
/**
* @param {string} name
*/
export function greet(name) {
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.greet(ptr0, len0);
}

let cachedInt32Memory0 = null;

function getInt32Memory0() {
Expand Down Expand Up @@ -210,11 +145,7 @@ export class World {
}
}

export function __wbg_alert_8eb924661f96b64d(arg0, arg1) {
alert(getStringFromWasm0(arg0, arg1));
};

export function __wbg_rnd_67ab8adcdd8659fa(arg0) {
export function __wbg_rnd_980494400e562b2a(arg0) {
const ret = rnd(arg0 >>> 0);
return ret;
};
Expand Down
Binary file modified crate/pkg/snake_bg.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions crate/pkg/snake_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_world_free(a: number): void;
export function world_new(a: number, b: number, c: number): number;
export function world_new(a: number, b: number, c: number, d: number): number;
export function world_points(a: number): number;
export function world_reward_cell(a: number, b: number): void;
export function world_width(a: number): number;
export function world_snake_head_idx(a: number): number;
export function world_snake_cells(a: number): number;
export function world_snake_cells(a: number, b: number): void;
export function world_snake_body_length(a: number): number;
export function world_update_direction(a: number, b: number): void;
export function world_play(a: number): void;
Expand Down
Loading