allow generateStructInfo to work at runtime with structMetadata

This commit is contained in:
Alon Zakai 2011-08-15 18:16:37 -07:00
Родитель 10cfcfa316
Коммит ea6f1cebc0
3 изменённых файлов: 10 добавлений и 4 удалений

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

@ -840,6 +840,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
if (RUNTIME_TYPE_INFO) {
Types.cleanForRuntime();
print('Runtime.typeInfo = ' + JSON.stringify(Types.types));
print('Runtime.structMetadata = ' + JSON.stringify(Types.structMetadata));
}
generated.forEach(function(item) { print(indentify(item.JS || '', 2)); });
print(Functions.generateIndexing());

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

@ -183,11 +183,11 @@ Runtime = {
var type, alignment;
if (typeName) {
offset = offset || 0;
type = typeof Types === 'undefined' ? Runtime.typeInfo[typeName] : Types.types[typeName];
type = (typeof Types === 'undefined' ? Runtime.typeInfo : Types.types)[typeName];
if (!type) return null;
if (!struct) struct = Types.structMetadata[typeName.replace(/.*\./, '')];
if (!struct) struct = (typeof Types === 'undefined' ? Runtime : Types).structMetadata[typeName.replace(/.*\./, '')];
if (!struct) return null;
assert(type.fields.length === struct.length, 'Number of named fields must match the type for ' + typeName);
assert(type.fields.length === struct.length, 'Number of named fields must match the type for ' + typeName + '. Perhaps due to inheritance, which is not supported yet?');
alignment = type.flatIndexes;
} else {
var type = { fields: struct.map(function(item) { return item[0] }) };

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

@ -3164,6 +3164,8 @@ if 'benchmark' not in sys.argv:
# Way 2: use CppHeaderParser
global RUNTIME_TYPE_INFO; RUNTIME_TYPE_INFO = 1
header = '''
#include <stdio.h>
@ -3339,13 +3341,16 @@ Child2:9
print('|' + Runtime.typeInfo.UserStruct.fields + '|' + Runtime.typeInfo.UserStruct.flatIndexes + '|');
var t = Runtime.generateStructInfo(['x', { us: ['x', 'y', 'z'] }, 'y'], 'Encloser')
print('|' + [t.x, t.us.x, t.us.y, t.us.z, t.y] + '|');
print('|' + JSON.stringify(Runtime.generateStructInfo(null, 'UserStruct')) + '|');
} else {
print('No type info.');
}
'''
)
open(filename, 'w').write(src)
self.do_test(src, '*ok:5*\n|i32,i8,i16|0,4,6|\n|0,4,8,10,12|', post_build=post)
self.do_test(src,
'*ok:5*\n|i32,i8,i16|0,4,6|\n|0,4,8,10,12|\n|{"__size__":8,"x":0,"y":4,"z":6}|',
post_build=post)
# Make sure that without the setting, we don't spam the .js with the type info
RUNTIME_TYPE_INFO = 0