@@ -319,6 +319,7 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface {
319319 extra);
320320 }
321321
322+ // We assume the table is not modified FIXME
322323 Literals callTable (Name tableName,
323324 Index index,
324325 HeapType sig,
@@ -333,8 +334,9 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface {
333334 throw FailToEvalException (" callTable on non-existing table" );
334335 }
335336
336- // we assume the table is not modified (hmm)
337- // look through the segments, try to find the function
337+ // Look through the segments and find the function. Segments can overlap,
338+ // so we want the last one.
339+ Name targetFunc;
338340 for (auto & segment : wasm->elementSegments ) {
339341 if (segment->table != tableName) {
340342 continue ;
@@ -357,29 +359,33 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface {
357359 if (start <= index && index < end) {
358360 auto entry = segment->data [index - start];
359361 if (auto * get = entry->dynCast <RefFunc>()) {
360- auto name = get->func ;
361- // if this is one of our functions, we can call it; if it was
362- // imported, fail
363- auto * func = wasm->getFunction (name);
364- if (func->type != sig) {
365- throw FailToEvalException (
366- std::string (" callTable signature mismatch: " ) + name.str );
367- }
368- if (!func->imported ()) {
369- return instance.callFunctionInternal (name, arguments);
370- } else {
371- throw FailToEvalException (
372- std::string (" callTable on imported function: " ) + name.str );
373- }
362+ targetFunc = get->func ;
374363 } else {
375364 throw FailToEvalException (
376365 std::string (" callTable on uninitialized entry" ));
377366 }
378367 }
379368 }
380- throw FailToEvalException (
381- std::string (" callTable on index not found in static segments: " ) +
382- std::to_string (index));
369+
370+ if (!targetFunc.is ()) {
371+ throw FailToEvalException (
372+ std::string (" callTable on index not found in static segments: " ) +
373+ std::to_string (index));
374+ }
375+
376+ // If this is one of our functions, we can call it; if it was
377+ // imported, fail.
378+ auto * func = wasm->getFunction (targetFunc);
379+ if (func->type != sig) {
380+ throw FailToEvalException (std::string (" callTable signature mismatch: " ) +
381+ targetFunc.str );
382+ }
383+ if (!func->imported ()) {
384+ return instance.callFunctionInternal (targetFunc, arguments);
385+ } else {
386+ throw FailToEvalException (
387+ std::string (" callTable on imported function: " ) + targetFunc.str );
388+ }
383389 }
384390
385391 Index tableSize (Name tableName) override {
0 commit comments