aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-12-10 11:10:47 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-12-10 11:10:47 -0800
commit539ce7ac862e60a8ae3cd94c59a24a8e992b0c64 (patch)
treeeb4299c34bd112767b51df0715f21e37b7109549
parentd69977b56d12b5e8077b8e118b050ac288099a34 (diff)
quote keys in library objects when necessary; fixes #1908
-rw-r--r--src/utility.js8
-rw-r--r--tests/test_other.py14
2 files changed, 21 insertions, 1 deletions
diff --git a/src/utility.js b/src/utility.js
index cd27b209..178c596b 100644
--- a/src/utility.js
+++ b/src/utility.js
@@ -346,13 +346,19 @@ function sortedJsonCompare(x, y) {
return true;
}
+function escapeJSONKey(x) {
+ if (/^[\d\w_]+$/.exec(x) || x[0] === '"' || x[0] === "'") return x;
+ assert(x.indexOf("'") < 0, 'cannot have internal single quotes in keys: ' + x);
+ return "'" + x + "'";
+}
+
function stringifyWithFunctions(obj) {
if (typeof obj === 'function') return obj.toString();
if (obj === null || typeof obj !== 'object') return JSON.stringify(obj);
if (isArray(obj)) {
return '[' + obj.map(stringifyWithFunctions).join(',') + ']';
} else {
- return '{' + keys(obj).map(function(key) { return key + ':' + stringifyWithFunctions(obj[key]) }).join(',') + '}';
+ return '{' + keys(obj).map(function(key) { return escapeJSONKey(key) + ':' + stringifyWithFunctions(obj[key]) }).join(',') + '}';
}
}
diff --git a/tests/test_other.py b/tests/test_other.py
index 5100db72..b10ae13b 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -2136,3 +2136,17 @@ int main()
assert 'test.o' in head, 'Invalid dependency target'
assert 'test.cpp' in tail and 'test.hpp' in tail, 'Invalid dependencies generated'
+ def test_quoted_js_lib_key(self):
+ open('lib.js', 'w').write(r'''
+mergeInto(LibraryManager.library, {
+ __internal_data:{
+ '<' : 0,
+ 'white space' : 1
+ },
+ printf__deps: ['__internal_data', 'fprintf']
+});
+''')
+
+ Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.cpp'), '--js-library', 'lib.js']).communicate()
+ self.assertContained('hello, world!', run_js(os.path.join(self.get_dir(), 'a.out.js')))
+