explicitly provide Module to EM_ASM scripts, so that they work even if Module was removed by closure; #2639

This commit is contained in:
Alon Zakai 2014-08-07 16:50:01 -07:00
Родитель 4264dab989
Коммит de2583876c
3 изменённых файлов: 70 добавлений и 67 удалений

Просмотреть файл

@ -420,7 +420,8 @@ var Runtime = {
}
#if NO_DYNAMIC_EXECUTION == 0
try {
var evalled = eval('(function(' + args.join(',') + '){ ' + source + ' })'); // new Function does not allow upvars in node
// Module is the only 'upvar', which we provide directly. We also provide FS for legacy support.
var evalled = eval('(function(Module, FS) { return function(' + args.join(',') + '){ ' + source + ' } })')(Module, typeof FS !== 'undefined' ? FS : null);
} catch(e) {
Module.printErr('error in executing inline EM_ASM code: ' + e + ' on: \n\n' + source + '\n\nwith args |' + args + '| (make sure to use the right one out of EM_ASM, EM_ASM_ARGS, etc.)');
throw e;

Просмотреть файл

@ -6293,7 +6293,9 @@ def process(filename):
assert os.path.exists('glue.cpp')
assert os.path.exists('glue.js')
open('export.js', 'w').write('''this['Module'] = Module;\n''')
# Export things on "TheModule". This matches the typical use pattern of the bound library
# being used as Box2D.* or Ammo.*, and we cannot rely on "Module" being always present (closure may remove it).
open('export.js', 'w').write('''this['TheModule'] = Module;\n''')
self.emcc_args += ['--post-js', 'glue.js', '--post-js', 'export.js']
shutil.copyfile(path_from_root('tests', 'webidl', 'test.h'), self.in_dir('test.h'))
shutil.copyfile(path_from_root('tests', 'webidl', 'test.cpp'), self.in_dir('test.cpp'))
@ -6301,7 +6303,7 @@ def process(filename):
def post(filename):
src = open(filename, 'a')
src.write('\n\n')
src.write('var Module = this.Module;\n')
src.write('var TheModule = this.TheModule;\n')
src.write('\n\n')
src.write(open(path_from_root('tests', 'webidl', 'post.js')).read())
src.write('\n\n')

Просмотреть файл

@ -1,157 +1,157 @@
// Part 1
var sme = new Module.Parent(42);
var sme = new TheModule.Parent(42);
sme.mulVal(2);
Module.print('*')
Module.print(sme.getVal());
TheModule.print('*')
TheModule.print(sme.getVal());
sme.parentFunc(90);
Module.print(typeof sme.getAsConst());
TheModule.print(typeof sme.getAsConst());
Module.print('c1');
TheModule.print('c1');
var c1 = new Module.Child1();
Module.print(c1.getVal());
var c1 = new TheModule.Child1();
TheModule.print(c1.getVal());
c1.mulVal(2);
Module.print(c1.getVal());
Module.print(c1.getValSqr());
Module.print(c1.getValSqr(3));
Module.print(c1.getValTimes()); // default argument should be 1
Module.print(c1.getValTimes(2));
TheModule.print(c1.getVal());
TheModule.print(c1.getValSqr());
TheModule.print(c1.getValSqr(3));
TheModule.print(c1.getValTimes()); // default argument should be 1
TheModule.print(c1.getValTimes(2));
c1.parentFunc(90);
Module.print('c1 v2');
TheModule.print('c1 v2');
c1 = new Module.Child1(8); // now with a parameter, we should handle the overloading automatically and properly and use constructor #2
Module.print(c1.getVal());
c1 = new TheModule.Child1(8); // now with a parameter, we should handle the overloading automatically and properly and use constructor #2
TheModule.print(c1.getVal());
c1.mulVal(2);
Module.print(c1.getVal());
Module.print(c1.getValSqr());
Module.print(c1.getValSqr(3));
TheModule.print(c1.getVal());
TheModule.print(c1.getValSqr());
TheModule.print(c1.getValSqr(3));
Module.print('c2')
TheModule.print('c2')
var c2 = new Module.Child2();
Module.print(c2.getVal());
var c2 = new TheModule.Child2();
TheModule.print(c2.getVal());
c2.mulVal(2);
Module.print(c2.getVal());
Module.print(c2.getValCube());
TheModule.print(c2.getVal());
TheModule.print(c2.getValCube());
var succeeded;
try {
succeeded = 0;
Module.print(c2.doSomethingSecret()); // should fail since private
TheModule.print(c2.doSomethingSecret()); // should fail since private
succeeded = 1;
} catch(e) {}
Module.print(succeeded);
TheModule.print(succeeded);
try {
succeeded = 0;
Module.print(c2.getValSqr()); // function from the other class
TheModule.print(c2.getValSqr()); // function from the other class
succeeded = 1;
} catch(e) {}
Module.print(succeeded);
TheModule.print(succeeded);
try {
succeeded = 0;
c2.getValCube(); // sanity
succeeded = 1;
} catch(e) {}
Module.print(succeeded);
TheModule.print(succeeded);
Module.Child2.prototype.printStatic(); // static calls go through the prototype
TheModule.Child2.prototype.printStatic(); // static calls go through the prototype
// virtual function
c2.virtualFunc();
Module.Child2.prototype.runVirtualFunc(c2);
TheModule.Child2.prototype.runVirtualFunc(c2);
c2.virtualFunc2();
// extend a class from JS
var c3 = new Module.Child2JS;
var c3 = new TheModule.Child2JS;
c3.virtualFunc = function() {
Module.print('*js virtualf replacement*');
TheModule.print('*js virtualf replacement*');
};
c3.virtualFunc2 = function() {
Module.print('*js virtualf2 replacement*');
TheModule.print('*js virtualf2 replacement*');
};
c3.virtualFunc3 = function(x) {
Module.print('*js virtualf3 replacement ' + x + '*');
TheModule.print('*js virtualf3 replacement ' + x + '*');
};
c3.virtualFunc();
Module.Child2.prototype.runVirtualFunc(c3);
TheModule.Child2.prototype.runVirtualFunc(c3);
c3.virtualFunc2();
c3.virtualFunc3(123); // this one is not replaced!
try {
c3.virtualFunc4(123);
} catch(e) {
Module.print('caught: ' + e);
TheModule.print('caught: ' + e);
}
// Test virtual method dispatch from c++
Module.Child2.prototype.runVirtualFunc3(c3, 43);
TheModule.Child2.prototype.runVirtualFunc3(c3, 43);
c2.virtualFunc(); // original should remain the same
Module.Child2.prototype.runVirtualFunc(c2);
TheModule.Child2.prototype.runVirtualFunc(c2);
c2.virtualFunc2();
Module.print('*ok*');
TheModule.print('*ok*');
// Part 2
var suser = new Module.StringUser("hello", 43);
var suser = new TheModule.StringUser("hello", 43);
suser.Print(41, "world");
suser.PrintFloat(12.3456);
var bv = new Module.RefUser(10);
var bv2 = new Module.RefUser(11);
Module.print(bv2.getValue(bv));
var bv = new TheModule.RefUser(10);
var bv2 = new TheModule.RefUser(11);
TheModule.print(bv2.getValue(bv));
Module.print(typeof bv2.getMe());
Module.print(bv2.getMe().getValue(bv));
Module.print(bv2.getMe().getValue(bv2));
TheModule.print(typeof bv2.getMe());
TheModule.print(bv2.getMe().getValue(bv));
TheModule.print(bv2.getMe().getValue(bv2));
Module.print(typeof bv2.getCopy());
Module.print(bv2.getCopy().getValue(bv));
Module.print(bv2.getCopy().getValue(bv2));
TheModule.print(typeof bv2.getCopy());
TheModule.print(bv2.getCopy().getValue(bv));
TheModule.print(bv2.getCopy().getValue(bv2));
bv2.getAnother().PrintFloat(21.12);
Module.print(new Module.Inner().get());
new Module.Inner().mul(2);
TheModule.print(new TheModule.Inner().get());
new TheModule.Inner().mul(2);
Module.print(Module.enum_value1);
Module.print(Module.enum_value2);
TheModule.print(TheModule.enum_value1);
TheModule.print(TheModule.enum_value2);
// Enums from classes are accessed via the class.
enumClassInstance = new Module.EnumClass();
Module.print([enumClassInstance.GetEnum(), Module.EnumClass.e_val].join(','));
enumClassInstance = new TheModule.EnumClass();
TheModule.print([enumClassInstance.GetEnum(), TheModule.EnumClass.e_val].join(','));
// Enums from namespaces are accessed via the top-level module, as with classes defined
// in namespaces, see `Inner` above.
Module.print(Module.e_namespace_val);
TheModule.print(TheModule.e_namespace_val);
typeTester = new Module.TypeTestClass();
typeTester = new TheModule.TypeTestClass();
Module.print('return char ' + typeTester.ReturnCharMethod());
TheModule.print('return char ' + typeTester.ReturnCharMethod());
typeTester.AcceptCharMethod((2<<6)-1);
// Prints -1 because the c++ code accepts unsigned char.
typeTester.AcceptCharMethod((2<<7)-1);
// Prints -1 because all integers are signed in javascript.
Module.print('return unsigned char ' + typeTester.ReturnUnsignedCharMethod());
TheModule.print('return unsigned char ' + typeTester.ReturnUnsignedCharMethod());
typeTester.AcceptUnsignedCharMethod((2<<7)-1);
// Prints -1 because all integers are signed in javascript.
Module.print('return unsigned short ' + typeTester.ReturnUnsignedShortMethod());
TheModule.print('return unsigned short ' + typeTester.ReturnUnsignedShortMethod());
typeTester.AcceptUnsignedShortMethod((2<<15)-1);
// Prints -1 because all integers are signed in javascript.
Module.print('return unsigned long ' + typeTester.ReturnUnsignedLongMethod());
TheModule.print('return unsigned long ' + typeTester.ReturnUnsignedLongMethod());
typeTester.AcceptUnsignedLongMethod((2<<31)-1);
var voidPointerUser = new Module.VoidPointerUser();
var voidPointerUser = new TheModule.VoidPointerUser();
voidPointerUser.SetVoidPointer(3);
Module.print('void * ' + voidPointerUser.GetVoidPointer());
TheModule.print('void * ' + voidPointerUser.GetVoidPointer());
//
Module.print('\ndone.')
TheModule.print('\ndone.')