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

Update as-pect and assemblyscript version #5

Open
wants to merge 5 commits into
base: master
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
9 changes: 0 additions & 9 deletions assembly/__tests__/example.spec.imports.js

This file was deleted.

13 changes: 10 additions & 3 deletions assembly/__tests__/example.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { degree, mod, Rabin } from '../index'

function create<T>(values: valueof<T>[]): T {
let result = instantiate<T>(values.length);
for (let i = 0; i < values.length; i++) result[i] = values[i];
return result;
}

@external("linked", "getFile")
declare function getFile(): Uint8Array;
function getFile(): Uint8Array {
return create<Uint8Array>([1,2,3,4,5,5,67,6,7,9]);
}


describe("rabin degree", (): void => {
Expand All @@ -17,7 +23,8 @@ describe("rabin degree", (): void => {
it("fingerprint", (): void => {
let r = new Rabin(14, 1 * 8, 2 * 8, 64)
let file = getFile()
r.fingerprint(file, new Int32Array(file.length/8))
let result = r.fingerprint(file)
// todo: inspect result
});

});
17 changes: 10 additions & 7 deletions assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "allocator/arena";

export { memory }

let tables_initialized: bool = false
Expand Down Expand Up @@ -177,10 +175,11 @@ export class Rabin {
rabin_init(this)
}

fingerprint(buf: Uint8Array, lengths: Int32Array): void {
fingerprint(buf: Uint8Array): i32[] {
let len = buf.length;
let chunk_idx = 0;
let ptr = buf.buffer.data
let ptr = changetype<usize>(buf.buffer)
let result = new Array<i32>(0);

while (1) {
var remaining = rabin_next_chunk(this, ptr, len);
if (remaining < 0) {
Expand All @@ -189,8 +188,12 @@ export class Rabin {

len -= remaining;
ptr += remaining;
let c = chunk_idx++
unchecked(lengths[c] = <i32>this.chunk_length)
result.push(<i32>this.chunk_length);
}
return result;
}
}

export function getUint8ArrayTypeId(): i32 {
return idof<Uint8Array>();
}
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,35 @@
"cli"
],
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize",
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug --runtime stub",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize --runtime stub",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"build": "asc assembly/index.ts -b dist/rabin.wasm -d dist/rabin.d.ts --sourceMap --validate -O3z && ./tools/wasm2js dist/rabin.wasm -o dist/rabin-wasm.js && browserify src/index.js -s Rabin -o dist/rabin.umd.js",
"size": "size-limit dist/rabin.umd.js",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"test": "asp"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hugomrdias/rabin-wasm.git"
},
"dependencies": {
"assemblyscript": "github:assemblyscript/assemblyscript#v0.6",
"bl": "^1.0.0",
"assemblyscript": "github:assemblyscript/assemblyscript",
"bl": "^3.0.0",
"browserify": "^16.3.0",
"debug": "^4.1.1",
"minimist": "^1.2.0",
"node-fetch": "^2.6.0",
"readable-stream": "^2.0.4"
"readable-stream": "^3.4.0"
},
"devDependencies": {
"as-pect": "github:jtenner/as-pect#52ed8fd923d0ad96a07fe1b52de6c51d49e3b246",
"@as-pect/cli": "2.3.1",
"benchmark": "^2.1.4",
"browserify": "^16.3.0",
"iso-random-stream": "^1.1.0",
"long": "^4.0.0",
"np": "^5.0.2",
"rabin": "^1.6.0",
"size-limit": "^1.3.5"
"np": "^5.0.3",
"rabin": "^2.0.0",
"size-limit": "^1.3.8"
}
}
25 changes: 8 additions & 17 deletions src/rabin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,19 @@ class Rabin {
* @memberof Rabin
*/
fingerprint(buf) {
const lengths = new Int32Array(Math.ceil(buf.length/this.min))
const lengthsPtr = this.asModule.newArray(lengths)
const pointer = this.asModule.newArray(buf)
const u8arraytype = this.asModule.getUint8ArrayTypeId()
const pointer = this.asModule.__retain(this.asModule.__allocArray(u8arraytype, buf))

// run finderprint
this.rabin.fingerprint(pointer, lengthsPtr)
const processedPointer = this.rabin.fingerprint(pointer)

const processed = this.asModule.getArray(Int32Array, lengthsPtr)
const processed = this.asModule.__getArray(processedPointer)

//free memory
this.asModule.freeArray(lengthsPtr)
this.asModule.freeArray(pointer)
// release memory
this.asModule.__release(pointer)
this.asModule.__release(processedPointer)

// TODO: remove this. @see https://github.com/ipfs/js-ipfs/issues/2118#issuecomment-497722625
// clean extra 0s in the array
const cleanArr = []
for (let i = 0; i < processed.length; i++) {
if(processed[i] === 0) break
cleanArr[i] = processed[i];
}

return cleanArr
return processed
}
}

Expand Down