@@ -1440,33 +1440,49 @@ class SExpressionWasmBuilder {
14401440 im->name = Name::fromInt (importCounter);
14411441 }
14421442 importCounter++;
1443+ if (!s[i]->quoted ()) {
1444+ if (s[i]->str () == MEMORY ) {
1445+ im->kind = Import::Memory;
1446+ } else if (s[2 ]->str () == TABLE ) {
1447+ im->kind = Import::Table;
1448+ } else if (s[2 ]->str () == GLOBAL ) {
1449+ im->kind = Import::Table;
1450+ } else {
1451+ WASM_UNREACHABLE ();
1452+ }
1453+ i++;
1454+ } else {
1455+ im->kind = Import::Function;
1456+ }
14431457 im->module = s[i++]->str ();
14441458 if (!s[i]->isStr ()) throw ParseException (" no name for import" );
14451459 im->base = s[i++]->str ();
1446- std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
1447- if (s.size () > i) {
1448- Element& params = *s[i];
1449- IString id = params[0 ]->str ();
1450- if (id == PARAM ) {
1451- for (size_t i = 1 ; i < params.size (); i++) {
1452- type->params .push_back (stringToWasmType (params[i]->str ()));
1460+ if (im->kind == Import::Function) {
1461+ std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
1462+ if (s.size () > i) {
1463+ Element& params = *s[i];
1464+ IString id = params[0 ]->str ();
1465+ if (id == PARAM ) {
1466+ for (size_t i = 1 ; i < params.size (); i++) {
1467+ type->params .push_back (stringToWasmType (params[i]->str ()));
1468+ }
1469+ } else if (id == RESULT ) {
1470+ type->result = stringToWasmType (params[1 ]->str ());
1471+ } else if (id == TYPE ) {
1472+ IString name = params[1 ]->str ();
1473+ if (!wasm.checkFunctionType (name)) throw ParseException (" bad function type for import" );
1474+ *type = *wasm.getFunctionType (name);
1475+ } else {
1476+ throw ParseException (" bad import element" );
1477+ }
1478+ if (s.size () > i+1 ) {
1479+ Element& result = *s[i+1 ];
1480+ assert (result[0 ]->str () == RESULT );
1481+ type->result = stringToWasmType (result[1 ]->str ());
14531482 }
1454- } else if (id == RESULT ) {
1455- type->result = stringToWasmType (params[1 ]->str ());
1456- } else if (id == TYPE ) {
1457- IString name = params[1 ]->str ();
1458- if (!wasm.checkFunctionType (name)) throw ParseException (" bad function type for import" );
1459- *type = *wasm.getFunctionType (name);
1460- } else {
1461- throw ParseException (" bad import element" );
1462- }
1463- if (s.size () > i+1 ) {
1464- Element& result = *s[i+1 ];
1465- assert (result[0 ]->str () == RESULT );
1466- type->result = stringToWasmType (result[1 ]->str ());
14671483 }
1484+ im->type = ensureFunctionType (getSig (type.get ()), &wasm);
14681485 }
1469- im->type = ensureFunctionType (getSig (type.get ()), &wasm);
14701486 wasm.addImport (im.release ());
14711487 }
14721488
0 commit comments