Skip to content

Commit

Permalink
Added JS generation with emscripten.
Browse files Browse the repository at this point in the history
Some work left to make sure non-ascii text isn't decompressed
to mojibake.
  • Loading branch information
Ed-von-Schleck committed Apr 18, 2014
1 parent dfce8d4 commit 52d6257
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CC=clang
FLAGS=$(CFLAGS) -std=c99 -O3 -Wall
SOURCES=shoco.c
OBJECTS=$(SOURCES:.c=.o)
Expand Down Expand Up @@ -49,3 +50,9 @@ tests: tests.o $(OBJECTS) $(HEADERS)
.PHONY: clean
clean:
rm *.o

.PHONY: js
js: shoco.js

shoco.js: $(OBJECTS) $(HEADERS)
emcc shoco.c -O3 -o $@ --closure 1 -s EXPORTED_FUNCTIONS="['_shoco_compress', '_shoco_decompress']" --pre-js pre.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ If you use _shoco_, or like it for whatever reason, I'd really love to hear from
License
-------
_shoco_ is published under the MIT license; see the LICENSE file for details.
_shoco_ is published under the MIT license; see the [LICENSE](LICENSE) file for details.
Authors
-------
Expand Down
32 changes: 32 additions & 0 deletions pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Module = {
'preRun': function() {
var _shoco_compress = Module['cwrap']('shoco_compress', 'number', ['string', 'number', 'number', 'number']);
var _shoco_decompress = Module['cwrap']('shoco_decompress', 'number', ['number', 'number', 'number', 'number']);

window['shoco'] = {
'compress': function(str_in) {
var out_heap = Module['_malloc'](str_in.length * 8);
var out_buffer = new Uint8Array(Module['HEAPU8']['buffer'], out_heap, str_in.length * 8);
var ret = _shoco_compress(str_in, 0, out_buffer.byteOffset, out_buffer.byteLength);
var result = new Uint8Array(out_buffer.subarray(0, ret));
Module['_free'](out_buffer.byteOffset);
return result;
},
'decompress': function(cmp) {
var out_heap = Module['_malloc'](cmp.length * 8);
var out_buffer = new Uint8Array(Module['HEAPU8']['buffer'], out_heap, cmp.length * 8);
var in_heap = Module['_malloc'](cmp.byteLength);
var in_buffer = new Uint8Array(Module['HEAPU8']['buffer'], in_heap, cmp.byteLength);
in_buffer.set(new Uint8Array(cmp.buffer));
var len = _shoco_decompress(in_buffer.byteOffset, cmp.byteLength, out_buffer.byteOffset, out_buffer.byteLength);
result = String.fromCharCode.apply(null, out_buffer.subarray(0, len));
Module['_free'](in_buffer.byteOffset);
Module['_free'](out_buffer.byteOffset);
return result;
}
}

if (typeof module !== "undefined")
module.exports = window['shoco'];
}
};
Loading

0 comments on commit 52d6257

Please sign in to comment.