aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-01-07 10:49:15 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-01-07 10:49:15 -0800
commitf034e3928783ff7a6491833f358c51cfb082b197 (patch)
treeb4291ba96c0017aa17c7dce56f16fa1f5a62447a
parent6344b5c8d41ee9489ad51b40f9b0bee770b1a22f (diff)
parenta4b3e03260b90e288925cc1dfd9d4d591bc7741c (diff)
Merge branch 'add-libuuid' of github.com:fadams/emscripten into incoming
Conflicts: tools/shared.py
-rw-r--r--src/closure-externs.js110
-rw-r--r--src/library_uuid.js140
-rw-r--r--src/modules.js2
-rw-r--r--src/struct_info.json12
-rw-r--r--system/include/uuid/uuid.h35
-rw-r--r--tests/test_browser.py27
-rw-r--r--tests/test_other.py1
-rw-r--r--tests/uuid/test.c69
-rw-r--r--tools/shared.py5
9 files changed, 399 insertions, 2 deletions
diff --git a/src/closure-externs.js b/src/closure-externs.js
new file mode 100644
index 00000000..a82aa669
--- /dev/null
+++ b/src/closure-externs.js
@@ -0,0 +1,110 @@
+/**
+ * This file contains definitions for things that we'd really rather the closure compiler *didn't* minify.
+ * See http://code.google.com/p/closure-compiler/wiki/FAQ#How_do_I_write_an_externs_file
+ * See also the discussion here: https://github.com/kripken/emscripten/issues/1979
+ *
+ * The closure_compiler() method in tools/shared.py refers to this file when calling closure.
+ */
+
+// Closure externs used by library_uuid.js
+
+/**
+ * @param {Array} typedArray
+ */
+crypto.getRandomValues = function(typedArray) {};
+
+/**
+ BEGIN_NODE_INCLUDE
+ var crypto = require('crypto');
+ END_NODE_INCLUDE
+ */
+
+/**
+ * @type {Object.<string,*>}
+ */
+var crypto = {};
+
+/**
+ * @param {number} size
+ * @param {function(Error, buffer.Buffer)} callback
+ */
+crypto.randomBytes = function(size, callback) {};
+
+
+// Closure externs used by library_sockfs.js
+
+/**
+ BEGIN_NODE_INCLUDE
+ var ws = require('ws');
+ END_NODE_INCLUDE
+ */
+
+/**
+ * @type {Object.<string,*>}
+ */
+var ws = {};
+
+/**
+ * @param {string} event
+ * @param {function()} callback
+ */
+ws.on = function(event, callback) {};
+
+/**
+ * @param {Object} data
+ * @param {Object} flags
+ * @param {function()=} callback
+ */
+ws.send = function(data, flags, callback) {};
+
+/**
+* @type {boolean}
+*/
+ws.binaryType;
+
+/**
+ * @type {Object.<string,*>}
+ */
+var wss = ws.Server;
+
+/**
+ * @param {string} event
+ * @param {function()} callback
+ */
+wss.on = function(event, callback) {};
+
+/**
+ * @param {function()} callback
+ */
+wss.broadcast = function(callback) {};
+
+/**
+* @type {Object.<string,*>}
+*/
+wss._socket;
+
+/**
+* @type {string}
+*/
+wss.url;
+
+/**
+* @type {string}
+*/
+wss._socket.remoteAddress;
+
+/**
+* @type {number}
+*/
+wss._socket.remotePort;
+
+/**
+* @type {Object.<string,*>}
+*/
+var flags = {};
+/**
+* @type {boolean}
+*/
+flags.binary;
+
+
diff --git a/src/library_uuid.js b/src/library_uuid.js
new file mode 100644
index 00000000..adfd1882
--- /dev/null
+++ b/src/library_uuid.js
@@ -0,0 +1,140 @@
+// Implementation of libuuid creating RFC4122 version 4 random UUIDs.
+
+mergeInto(LibraryManager.library, {
+ // Clear a 'compact' UUID.
+ uuid_clear: function(uu) {
+ // void uuid_clear(uuid_t uu);
+ _memset(uu, 0, 16);
+ },
+
+ // Compare whether or not two 'compact' UUIDs are the same.
+ // Returns an integer less than, equal to, or greater than zero if uu1 is found, respectively, to be
+ // lexigraphically less than, equal, or greater than uu2.
+ uuid_compare__deps: ['memcmp'],
+ uuid_compare: function(uu1, uu2) {
+ // int uuid_compare(const uuid_t uu1, const uuid_t uu2);
+ return _memcmp(uu1, uu2, 16);
+ },
+
+ // Copies the 'compact' UUID variable from src to dst.
+ uuid_copy: function(dst, src) {
+ // void uuid_copy(uuid_t dst, const uuid_t src);
+ _memcpy(dst, src, 16);
+ },
+
+ // Write a RFC4122 version 4 compliant UUID largely based on the method found in
+ // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
+ // tweaked slightly in order to use the 'compact' UUID form used by libuuid.
+ uuid_generate: function(out) {
+ // void uuid_generate(uuid_t out);
+ var uuid = null;
+
+ if (ENVIRONMENT_IS_NODE) {
+ // If Node.js try to use crypto.randomBytes
+ try {
+ var rb = require('crypto').randomBytes;
+ uuid = rb(16);
+ } catch(e) {}
+ } else if (ENVIRONMENT_IS_WEB &&
+ typeof(window.crypto) !== 'undefined' &&
+ typeof(window.crypto.getRandomValues) !== 'undefined') {
+ // If crypto.getRandomValues is available try to use it.
+ uuid = new Uint8Array(16);
+ window.crypto.getRandomValues(uuid);
+ }
+
+ // Fall back to Math.random if a higher quality random number generator is not available.
+ if (!uuid) {
+ uuid = new Array(16);
+ var d = new Date().getTime();
+ for (var i = 0; i < 16; i++) {
+ var r = (d + Math.random()*256)%256 | 0;
+ d = Math.floor(d/256);
+ uuid[i] = r;
+ }
+ }
+
+ uuid[6] = (uuid[6] & 0x0F) | 0x40;
+ uuid[8] = (uuid[8] & 0x7F) | 0x80;
+ writeArrayToMemory(uuid, out);
+ },
+
+ // Compares the value of the supplied 'compact' UUID variable uu to the NULL value.
+ // If the value is equal to the NULL UUID, 1 is returned, otherwise 0 is returned.
+ uuid_is_null: function(uu) {
+ // int uuid_is_null(const uuid_t uu);
+ for (var i = 0; i < 4; i++, uu = (uu+4)|0) {
+ var val = {{{ makeGetValue('uu', 0, 'i32') }}};
+ if (val) {
+ return 0;
+ }
+ }
+ return 1;
+ },
+
+ // converts the UUID string given by inp into the binary representation. The input UUID is a string of
+ // the form "%08x-%04x-%04x-%04x-%012x" 36 bytes plus the trailing '\0'.
+ // Upon successfully parsing the input string, 0 is returned, and the UUID is stored in the location
+ // pointed to by uu, otherwise -1 is returned.
+ uuid_parse: function(inp, uu) {
+ // int uuid_parse(const char *in, uuid_t uu);
+ var inp = Pointer_stringify(inp);
+ if (inp.length === 36) {
+ var i = 0;
+ var uuid = new Array(16);
+ inp.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) {
+ if (i < 16) {
+ uuid[i++] = parseInt(byte, 16);
+ }
+ });
+
+ if (i < 16) {
+ return -1;
+ } else {
+ writeArrayToMemory(uuid, uu);
+ return 0;
+ }
+ } else {
+ return -1;
+ }
+ },
+
+ // Convert a 'compact' form UUID to a string, if the upper parameter is supplied make the string upper case.
+ uuid_unparse: function(uu, out, upper) {
+ // void uuid_unparse(const uuid_t uu, char *out);
+ var i = 0;
+ var uuid = 'xxxx-xx-xx-xx-xxxxxx'.replace(/[x]/g, function(c) {
+ var r = upper ? ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16).toUpperCase() :
+ ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16);
+ r = (r.length === 1) ? '0' + r : r; // Zero pad single digit hex values
+ i++;
+ return r;
+ });
+ writeStringToMemory(uuid, out);
+ },
+
+ // Convert a 'compact' form UUID to a lower case string.
+ uuid_unparse_lower__deps: ['uuid_unparse'],
+ uuid_unparse_lower: function(uu, out) {
+ // void uuid_unparse_lower(const uuid_t uu, char *out);
+ _uuid_unparse(uu, out);
+ },
+
+ // Convert a 'compact' form UUID to an upper case string.
+ uuid_unparse_upper__deps: ['uuid_unparse'],
+ uuid_unparse_upper: function(uu, out) {
+ // void uuid_unparse_upper(const uuid_t uu, char *out);
+ _uuid_unparse(uu, out, true);
+ },
+
+ uuid_type: function(uu) {
+ // int uuid_type(const uuid_t uu);
+ return {{{ cDefine('UUID_TYPE_DCE_RANDOM') }}};
+ },
+
+ uuid_variant: function(uu) {
+ // int uuid_variant(const uuid_t uu);
+ return {{{ cDefine('UUID_VARIANT_DCE') }}};
+ }
+});
+
diff --git a/src/modules.js b/src/modules.js
index e2d3433f..c0b98f6f 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -424,7 +424,7 @@ var LibraryManager = {
load: function() {
if (this.library) return;
- var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js'].concat(additionalLibraries);
+ var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js', 'library_uuid.js'].concat(additionalLibraries);
for (var i = 0; i < libraries.length; i++) {
eval(processMacros(preprocess(read(libraries[i]))));
}
diff --git a/src/struct_info.json b/src/struct_info.json
index c136cc8b..a22851b6 100644
--- a/src/struct_info.json
+++ b/src/struct_info.json
@@ -1061,5 +1061,17 @@
"patch"
]
}
+ },
+ {
+ "file": "uuid/uuid.h",
+ "defines": [
+ "UUID_VARIANT_NCS",
+ "UUID_VARIANT_DCE",
+ "UUID_VARIANT_MICROSOFT",
+ "UUID_VARIANT_OTHER",
+ "UUID_TYPE_DCE_TIME",
+ "UUID_TYPE_DCE_RANDOM"
+ ],
+ "structs": {}
}
]
diff --git a/system/include/uuid/uuid.h b/system/include/uuid/uuid.h
new file mode 100644
index 00000000..0a84e02c
--- /dev/null
+++ b/system/include/uuid/uuid.h
@@ -0,0 +1,35 @@
+
+#ifndef _UUID_H
+#define _UUID_H
+
+typedef unsigned char uuid_t[16];
+
+#define UUID_VARIANT_NCS 0
+#define UUID_VARIANT_DCE 1
+#define UUID_VARIANT_MICROSOFT 2
+#define UUID_VARIANT_OTHER 3
+
+#define UUID_TYPE_DCE_TIME 1
+#define UUID_TYPE_DCE_RANDOM 4
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void uuid_clear(uuid_t uu);
+int uuid_compare(const uuid_t uu1, const uuid_t uu2);
+void uuid_copy(uuid_t dst, const uuid_t src);
+void uuid_generate(uuid_t out);
+int uuid_is_null(const uuid_t uu);
+int uuid_parse(const char *in, uuid_t uu);
+void uuid_unparse(const uuid_t uu, char *out);
+void uuid_unparse_lower(const uuid_t uu, char *out);
+void uuid_unparse_upper(const uuid_t uu, char *out);
+int uuid_type(const uuid_t uu);
+int uuid_variant(const uuid_t uu);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _UUID_H */
diff --git a/tests/test_browser.py b/tests/test_browser.py
index 20b38bb5..c2eaabb6 100644
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@ -1720,3 +1720,30 @@ keydown(100);keyup(100); // trigger the end
assert 'argv[3]: 3' in stdout
assert 'hello, world!' in stdout
assert 'hello, error stream!' in stderr
+
+ def test_uuid(self):
+ # Run with ./runner.py browser.test_uuid
+ # We run this test in Node/SPIDERMONKEY and browser environments because we try to make use of
+ # high quality crypto random number generators such as crypto.getRandomValues or randomBytes (if available).
+
+ # First run tests in Node and/or SPIDERMONKEY using run_js. Use closure compiler so we can check that
+ # require('crypto').randomBytes and window.crypto.getRandomValues doesn't get minified out.
+ Popen([PYTHON, EMCC, '-O2', '--closure', '1', path_from_root('tests', 'uuid', 'test.c'), '-o', path_from_root('tests', 'uuid', 'test.js')], stdout=PIPE, stderr=PIPE).communicate()
+
+ test_js_closure = open(path_from_root('tests', 'uuid', 'test.js')).read()
+
+ # Check that test.js compiled with --closure 1 contains ").randomBytes" and "window.crypto.getRandomValues"
+ assert ").randomBytes" in test_js_closure
+ assert "window.crypto.getRandomValues" in test_js_closure
+
+ out = run_js(path_from_root('tests', 'uuid', 'test.js'), full_output=True)
+ print out
+
+ # Tidy up files that might have been created by this test.
+ try_delete(path_from_root('tests', 'uuid', 'test.js'))
+ try_delete(path_from_root('tests', 'uuid', 'test.js.map'))
+
+ # Now run test in browser
+ self.btest(path_from_root('tests', 'uuid', 'test.c'), '1')
+
+
diff --git a/tests/test_other.py b/tests/test_other.py
index 9c983f9f..f9ee2d9d 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -2209,3 +2209,4 @@ mergeInto(LibraryManager.library, {
process = Popen([PYTHON, EMCC, '-c', path_from_root('tests', 'hello_world.c'), '-o', outdir, '--default-obj-ext', 'obj'], stdout=PIPE, stderr=PIPE)
process.communicate()
assert(os.path.isfile(outdir + 'hello_world.obj'))
+
diff --git a/tests/uuid/test.c b/tests/uuid/test.c
new file mode 100644
index 00000000..dc2c6589
--- /dev/null
+++ b/tests/uuid/test.c
@@ -0,0 +1,69 @@
+#include <uuid/uuid.h>
+#include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <emscripten.h>
+
+int isUUID(char* p, int upper) {
+ char* p1 = p;
+ do {
+ if (!(isxdigit(*p1) || (*p1 == '-')) || (upper && islower(*p1)) || (!upper && isupper(*p1))) {
+ return 0;
+ } else {
+ }
+ } while (*++p1 != 0);
+
+ if ((p[8] == '-') && (p[13] == '-') && (p[18] == '-') && (p[23] == '-')) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int main() {
+ uuid_t uuid;
+ uuid_t uuid1;
+ uuid_t uuid2;
+ uuid_t empty_uuid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ uuid_generate(uuid);
+
+ assert(uuid_is_null(uuid) == 0);
+ assert(uuid_type(uuid) == UUID_TYPE_DCE_RANDOM);
+ assert(uuid_variant(uuid) == UUID_VARIANT_DCE);
+
+ char *generated = (char *)malloc(37*sizeof(char));
+ uuid_unparse(uuid, generated);
+ assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string.
+ printf("\nuuid = %s\n", generated);
+
+ assert(uuid_parse(generated, uuid1) == 0); // Check the generated UUID parses correctly into a compact UUID.
+ assert(uuid_compare(uuid1, uuid) == 0); // Compare the parsed UUID with the original.
+
+ uuid_unparse_lower(uuid, generated);
+ assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string.
+ printf("uuid = %s\n", generated);
+
+ uuid_unparse_upper(uuid, generated);
+ assert(isUUID(generated, 1) == 1); // Check it's a valid upper case UUID string.
+ printf("uuid = %s\n", generated);
+
+
+ uuid_copy(uuid2, uuid);
+ assert(uuid_compare(uuid2, uuid) == 0);
+
+ uuid_clear(uuid);
+ assert(uuid_compare(empty_uuid, uuid) == 0);
+
+ assert(uuid_is_null(uuid) == 1);
+
+ // The following lets the browser test exit cleanly.
+ int result = 1;
+ #if EMSCRIPTEN
+ #ifdef REPORT_RESULT
+ REPORT_RESULT();
+ #endif
+ #endif
+ exit(0);
+}
+
diff --git a/tools/shared.py b/tools/shared.py
index bd0626dc..363b29f9 100644
--- a/tools/shared.py
+++ b/tools/shared.py
@@ -337,7 +337,7 @@ def find_temp_directory():
# we re-check sanity when the settings are changed)
# We also re-check sanity and clear the cache when the version changes
-EMSCRIPTEN_VERSION = '1.8.3'
+EMSCRIPTEN_VERSION = '1.8.4'
def generate_sanity():
return EMSCRIPTEN_VERSION + '|' + get_llvm_target() + '|' + LLVM_ROOT
@@ -1404,6 +1404,8 @@ class Building:
if not os.path.exists(CLOSURE_COMPILER):
raise Exception('Closure compiler appears to be missing, looked at: ' + str(CLOSURE_COMPILER))
+ CLOSURE_EXTERNS = path_from_root('src', 'closure-externs.js')
+
# Something like this (adjust memory as needed):
# java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js
args = [JAVA,
@@ -1411,6 +1413,7 @@ class Building:
'-jar', CLOSURE_COMPILER,
'--compilation_level', 'ADVANCED_OPTIMIZATIONS',
'--language_in', 'ECMASCRIPT5',
+ '--externs', CLOSURE_EXTERNS,
#'--variable_map_output_file', filename + '.vars',
'--js', filename, '--js_output_file', filename + '.cc.js']
if pretty: args += ['--formatting', 'PRETTY_PRINT']