Skip to content

Instantly share code, notes, and snippets.

View tpmccallum's full-sized avatar

Timothy McCallum tpmccallum

View GitHub Profile
function isSubset(haystack, needle) {
return new Promise(function(resolve, reject) {
let n = haystack.length;
let m = needle.length;
for (let i = 0, j = 0; i < n; i++) {
if (haystack[i] === needle[j++]) {
if (j >= m) resolve("Present");
} else j = 0
}
resolve("Absent");
function isSubset(_haystack, _needle, _haystack_length, _needle_length) {
return new Promise(function(resolve, reject) {
var i = 0;
var j = 0;
for (i = 0; i < _needle_length; i++) {
for (j = 0; j < _haystack_length; j++) {
if (_needle[i] == _haystack[j]) {
break;
}
}
pub fn search_bytes_single_input(byte_array: &[u8]) -> String {
let mut number_of_bytes_str = "".to_string();
for i in 0..10 {
number_of_bytes_str.push_str(&byte_array[i].to_string());
}
let number_of_bytes_usize: usize = number_of_bytes_str.parse().unwrap();
let needle: &[u8] = &byte_array.get(10..10 + number_of_bytes_usize).unwrap();
let haystack: &[u8] = &byte_array.get(10 + number_of_bytes_usize..).unwrap();
match haystack.windows(needle.len()).position(|window| window == needle){
Some(_) => "Present".to_string(),
var buffer_1 = new ArrayBuffer(1000000);
var buffer_2 = new ArrayBuffer(1000000);
var needle = new Uint8Array(buffer_1);
var haystack = new Uint8Array(buffer_2);
needle.fill(111)
haystack.fill(222)
needle_length = needle.length;
haystack_length = haystack.length;
needle_length_string = needle_length.toString();
for (i = needle_length_string.length; i < 10; i++) {
@tpmccallum
tpmccallum / oasis_token_list.json
Created November 7, 2020 02:01
Balancer requires a list of token addresses
{
"tokens": {
"ether": {
"address": "ether",
"name": "Ether",
"symbol": "OETH",
"decimals": 18,
"precision": 4,
"hasIcon": true,
"logoUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png"
@tpmccallum
tpmccallum / single_variable_game_state.md
Last active February 22, 2020 02:02
An idea that allows a simple computer game's state to be stored in one variable only

Idea

In the context of a computer game (where each player or the game board etc. holds a "state") rather than having an array of variables or storing small[ish] values (like 0 or 1 or 2 etc) in separate variables (like u32) we could treat one variable as many by turning that single variable into a multi-slot representation through the use of modulo arithmetic and exponentiation.

Question

Hand writing memory in WebAssembly is possible. But if we are only writing in Rust and treating Wasm as a compilation target could the following idea be implemented as a design pattern for higher level programming i.e. Rust? Or, is the compiler so smart that this idea is redundant (after all of the wonderful optimisations and magic that compilers perform)?

Example

The u32 variable in Rust can hold a value within the range of 0 to 2147483648. We essentially have access to 10 separate slots. If we initialise the variable with 1000000000 (first digit set to 1) then any of the remaining 9 slots can each hold a sepa

Standard code

The following test (is a baseline before adding any changes in relation to PR 1987). These tests were performed using the standard untouched code base from https://github.com/rustwasm/wasm-bindgen.git

cargo test -p wasm-bindgen-cli --test reference
    Finished test [unoptimized + debuginfo] target(s) in 0.12s
     Running target/debug/deps/reference-c3db2c64df48e6fd
failed tests:

/Users/tpmccallum/wasm-bindgen/crates/cli/tests/reference/anyref-empty.rs failure
    found a difference:
cargo test -p wasm-bindgen-cli


tpmccallum$ cargo test -p wasm-bindgen-cli --test reference
    Finished test [unoptimized + debuginfo] target(s) in 0.27s
     Running /Users/tpmccallum/wasm-bindgen/target/debug/deps/reference-fa94d6404d107293
failed tests:

/Users/tpmccallum/wasm-bindgen/crates/cli/tests/reference/anyref-empty.rs failure
tpmccallum$ cargo test --target wasm32-unknown-unknown 
    Finished test [unoptimized + debuginfo] target(s) in 0.10s
     Running target/wasm32-unknown-unknown/debug/deps/headless-f2eded3f78b0be11.wasm
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/wasm-bindgen-test-runner /Users/tpmccallum/wasm-bindgen/target/wasm32-unknown-unknown/debug/deps/headless-f2eded3f78b0be11.wasm`
Running headless tests in Safari on `http://127.0.0.1:52627/`
Try find `webdriver.json` for configure browser's capabilities:
Ok
running 10 tests                                  
running 10 tests

test headless::closures_work ... ok
test headless::can_log_html_strings ... ok
test headless::works ... ok
test headless::modules::test_get_five ... ok
test headless::snippets::duplicate_inline_not_unified ... ok
test headless::snippets::test_get_three ... ok
test headless::snippets::stateful_deduplicated ... ok