diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-07-17 22:06:26 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-07-17 22:06:26 -0700 |
commit | 9bcf21de1fff8ed137e66b04c23fd8308b0ae89a (patch) | |
tree | a00dccf632560647dff6cb5c5727185e29a355f6 /src | |
parent | 756e3e1ae499bc8849cc82ef3bf33f55fe767ef6 (diff) |
let generateStructInfo process nested structs
Diffstat (limited to 'src')
-rw-r--r-- | src/modules.js | 1 | ||||
-rw-r--r-- | src/runtime.js | 31 |
2 files changed, 25 insertions, 7 deletions
diff --git a/src/modules.js b/src/modules.js index dc4a296d..89684411 100644 --- a/src/modules.js +++ b/src/modules.js @@ -134,7 +134,6 @@ var Types = { if (shorter === longer) return; if (shorter in this.types) return; this.types[shorter] = this.types[longer]; - delete this.types[longer]; }, this); }, diff --git a/src/runtime.js b/src/runtime.js index 559dd125..06638fa3 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -167,11 +167,17 @@ Runtime = { // generateStructInfo(['field1', 'field2'], '%struct.UserStructType'); // (Note that you will need the full %struct.* name here at compile time, // but not at runtime. The reason is that during compilation we cannot simplify - // the type names yet.) - generateStructInfo: function(struct, typeName) { + // the type names yet. At runtime, you can provide either the short or the + // full name.) + // + // When providing a typeName, you can generate information for nested + // structs, for example, struct = ['field1', { field2: ['sub1', 'sub2', 'sub3'] }, 'field3'] + // which repesents a structure whose 2nd field is another structure. + generateStructInfo: function(struct, typeName, offset) { var type, alignment; if (typeName) { - type = Types.types[typeName]; + offset = offset || 0; + type = typeof Types === 'undefined' ? Runtime.typeInfo[typeName] : Types.types[typeName]; if (!type) return null; assert(type.fields.length === struct.length, 'Number of named fields must match the type for ' + typeName); alignment = type.flatIndexes; @@ -182,9 +188,22 @@ Runtime = { var ret = { __size__: type.flatSize }; - struct.forEach(function(item, i) { - ret[typeof item === 'string' ? item : item[1]] = alignment[i]; - }); + if (typeName) { + struct.forEach(function(item, i) { + if (typeof item === 'string') { + ret[item] = alignment[i] + offset; + } else { + // embedded struct + var key; + for (var k in item) key = k; + ret[key] = Runtime.generateStructInfo(item[key], type.fields[i], alignment[i]); + } + }); + } else { + struct.forEach(function(item, i) { + ret[item[1]] = alignment[i]; + }); + } return ret; } }; |