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

embind: Remove str-to-func eval of named function factory #18748

Merged
merged 1 commit into from
Feb 15, 2023
Merged
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
23 changes: 7 additions & 16 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,16 @@ var LibraryEmbind = {
},


// from https://github.com/imvu/imvujs/blob/master/src/function.js
$createNamedFunction__deps: ['$makeLegalFunctionName'],
$createNamedFunction: function(name, body) {
name = makeLegalFunctionName(name);
#if DYNAMIC_EXECUTION == 0
return function() {
"use strict";
return body.apply(this, arguments);
};
#else
/*jshint evil:true*/
return new Function(
"body",
"return function " + name + "() {\n" +
" \"use strict\";" +
" return body.apply(this, arguments);\n" +
"};\n"
)(body);
#endif
// Use an abject with a computed property name to create a new function with
// a name specified at runtime, but without using `new Function` or `eval`.
return {
[name]: function() {
return body.apply(this, arguments);
}
}[name];
},

$embindRepr: function(v) {
Expand Down
11 changes: 4 additions & 7 deletions test/embind/embind.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,16 @@ module({
});

test("setting and getting property on unrelated class throws error", function() {
var className = cm['DYNAMIC_EXECUTION'] ? 'HasTwoBases' : '';
var a = new cm.HasTwoBases;
var e = assert.throws(cm.BindingError, function() {
Object.getOwnPropertyDescriptor(cm.HeldBySmartPtr.prototype, 'i').set.call(a, 10);
});
assert.equal('HeldBySmartPtr.i setter incompatible with "this" of type ' + className, e.message);
assert.equal('HeldBySmartPtr.i setter incompatible with "this" of type HasTwoBases', e.message);

var e = assert.throws(cm.BindingError, function() {
Object.getOwnPropertyDescriptor(cm.HeldBySmartPtr.prototype, 'i').get.call(a);
});
assert.equal('HeldBySmartPtr.i getter incompatible with "this" of type ' + className, e.message);
assert.equal('HeldBySmartPtr.i getter incompatible with "this" of type HasTwoBases', e.message);

a.delete();
});
Expand Down Expand Up @@ -1766,8 +1765,7 @@ module({

test("smart pointer object has correct constructor name", function() {
var e = new cm.HeldBySmartPtr(10, "foo");
var expectedName = cm['DYNAMIC_EXECUTION'] ? "HeldBySmartPtr" : "";
assert.equal(expectedName, e.constructor.name);
assert.equal("HeldBySmartPtr", e.constructor.name);
e.delete();
});

Expand Down Expand Up @@ -2510,12 +2508,11 @@ module({
});

BaseFixture.extend("function names", function() {
assert.equal('ValHolder', cm.ValHolder.name);
if (!cm['DYNAMIC_EXECUTION']) {
assert.equal('', cm.ValHolder.name);
assert.equal('', cm.ValHolder.prototype.setVal.name);
assert.equal('', cm.ValHolder.makeConst.name);
} else {
assert.equal('ValHolder', cm.ValHolder.name);
assert.equal('ValHolder$setVal', cm.ValHolder.prototype.setVal.name);
assert.equal('ValHolder$makeConst', cm.ValHolder.makeConst.name);
}
Expand Down
19 changes: 13 additions & 6 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -12370,6 +12370,13 @@ def test_es5_transpile(self, args):
// arror funcs + const
const bar = () => 2;
err('bar: ' + bar());

// Computed property names
var key = 'mykey';
var obj2 = {
[key]: 42,
};
err('value: ' + obj2[key]);
}
});
''')
Expand Down Expand Up @@ -12403,19 +12410,19 @@ def check_for_es6(filename, expect):
print('with old browser')
self.emcc_args.remove('-Werror')
self.set_setting('MIN_CHROME_VERSION', '10')
self.do_runf('test.c', 'prop: 1\nbar: 2\n', output_basename='test2')
check_for_es6('test2.js', False)
self.do_runf('test.c', 'prop: 1\nbar: 2\n', output_basename='test_old')
check_for_es6('test_old.js', False)

# If we add `--closure=0` that transpiler (closure) is not run at all
print('with old browser + --closure=0')
self.do_runf('test.c', 'prop: 1\nbar: 2\n', emcc_args=['--closure=0'], output_basename='test3')
check_for_es6('test3.js', True)
self.do_runf('test.c', 'prop: 1\nbar: 2\n', emcc_args=['--closure=0'], output_basename='test_no_closure')
check_for_es6('test_no_closure.js', True)

# If we use `--closure=1` closure will run in full optimization mode
# and also transpile to ES5
print('with old browser + --closure=1')
self.do_runf('test.c', 'prop: 1\nbar: 2\n', emcc_args=['--closure=1'], output_basename='test4')
check_for_es6('test4.js', False)
self.do_runf('test.c', 'prop: 1\nbar: 2\n', emcc_args=['--closure=1'], output_basename='test_closure')
check_for_es6('test_closure.js', False)

def test_gmtime_noleak(self):
# Confirm that gmtime_r does not leak when called in isolation.
Expand Down