DefinitelyTyped/types/emscripten/emscripten-tests.ts
Kensuke Matsuzaki a971b995f2 Rebase Fix and improvements for 1.38.33 (#37459)
* Improve types of `ccall` and `cwrap` functions

* Fix type of FS.init() since it accepts null

* Simplify type of writeFile()

* Add missing onRuntimeInitialized hook

* Add options to ccall() and cwrap()

* More tests

* Fix linter error and compile error

* Add version to header

* Fix string conversion functions are defined at toplevel

* Add me to maintainers

* Add lengthBytesUTF*() and allocateUTF8()

* Add Module.onAbort

* Fix emscripten lint

* Fix emscripten FS typing

* Revert "Add me to maintainers"

This reverts commit db3b5157b4ee35a9404cff6b673eed89cd42fc0f.
2019-08-09 07:24:02 -07:00

121 lines
3.6 KiB
TypeScript

/// Module
function ModuleTest(): void {
Module.environment = "WEB";
Module.environment = "NODE";
Module.noInitialRun = false;
Module.logReadFiles = false;
Module.filePackagePrefixURL = "http://www.example.org/";
Module.preinitializedWebGLContext = new WebGLRenderingContext();
Module.onAbort = (what) => console.log('abort');
Module.onRuntimeInitialized = () => console.log('init');
const package: ArrayBuffer = Module.getPreloadedPackage("package-name", 100);
const exports: Emscripten.WebAssemblyExports = Module.instantiateWasm(
[{name: "func-name", kind: "function"}],
(module: WebAssembly.Module) => {}
);
const memFile: string = Module.locateFile("http://www.example.org/file.mem");
Module.onCustomMessage(new MessageEvent("TestType"));
Module.print = (text) => alert('stdout: ' + text);
let int_sqrt = Module.cwrap('int_sqrt', 'number', ['number']);
int_sqrt = Module.cwrap('int_sqrt', null, ['number']);
int_sqrt(12);
int_sqrt(28);
const myTypedArray = new Uint8Array(10);
const buf = Module._malloc(myTypedArray.length * myTypedArray.BYTES_PER_ELEMENT);
Module.setValue(buf, 10, 'i32');
const x = Module.getValue(buf, 'i32') + 123;
Module.HEAPU8.set(myTypedArray, buf);
Module.ccall('my_function', 'number', ['number'], [buf]);
Module.ccall('my_function', null, ['number'], [buf]);
Module.ccall('my_function', null, ['number'], [buf], {async: true});
Module.cwrap('my_function', 'string', ['number', 'boolean', 'array']);
Module.cwrap('my_function', null, ['number']);
Module.cwrap('my_function', 'string', ['number', 'boolean', 'array'], {async: true});
Module._free(buf);
Module.destroy({});
}
/// FS
function FSTest(): void {
FS.init(() => null, _ => null, _ => null);
FS.init(null, null, null);
FS.mkdir('/working');
FS.mount(NODEFS, { root: '.' }, '/working');
function myAppStartup(): void {
FS.mkdir('/data');
FS.mount(IDBFS, {}, '/data');
FS.syncfs(true, (err) => {
// handle callback
});
}
function myAppShutdown() {
FS.syncfs((err) => {
// handle callback
});
}
const id = FS.makedev(64, 0);
FS.registerDevice(id, {});
FS.mkdev('/dummy', id);
FS.writeFile('file', 'foobar');
FS.symlink('file', 'link');
FS.writeFile('/foobar.txt', 'Hello, world');
FS.unlink('/foobar.txt');
FS.writeFile('file', 'foobar');
FS.symlink('file', 'link');
FS.writeFile('forbidden', 'can\'t touch this');
FS.chmod('forbidden', parseInt("0000", 8));
FS.writeFile('file', 'foobar');
FS.truncate('file', 3);
const contents = FS.readFile('file', { encoding: 'utf8' });
const rstream = FS.open('abinaryfile', 'r');
const buf = new Uint8Array(4);
FS.read(rstream, buf, 0, 4, 0);
FS.close(rstream);
const data = new Uint8Array(32);
const wstream = FS.open('dummy', 'w+');
FS.write(wstream, data, 0, data.length, 0);
FS.close(wstream);
const lookup = FS.lookupPath("path", { parent: true });
}
/// String conversions
function StringConv(): void {
let s = '';
let p = 0;
const nullptr = 0;
s = UTF8ToString(nullptr);
s = UTF8ToString(nullptr, 42);
s = UTF16ToString(nullptr);
s = UTF32ToString(nullptr);
const i1 = lengthBytesUTF8(s);
const i2 = lengthBytesUTF16(s);
const i3 = lengthBytesUTF32(s);
stringToUTF8(s, p);
stringToUTF8(s, p, 42);
stringToUTF16(s, p);
stringToUTF16(s, p, 42);
stringToUTF32(s, p);
stringToUTF32(s, p, 42);
p = allocateUTF8(s);
Module._free(p);
}