| 510 | } |
| 511 | |
| 512 | bool Compiler::AddModuleFunction(const char* module, void (NCDECL *ptr)(), const char* name, int index) |
| 513 | { |
| 514 | char errBuf[256]; |
| 515 | |
| 516 | // Find module |
| 517 | const char *importPath = BinaryCache::GetImportPath(); |
| 518 | char path[256], *pathNoImport = path, *cPath = path; |
| 519 | cPath += SafeSprintf(path, 256, "%s%.*s", importPath ? importPath : "", module, module); |
| 520 | if(importPath) |
| 521 | pathNoImport = path + strlen(importPath); |
| 522 | |
| 523 | for(unsigned int i = 0, e = (unsigned int)strlen(path); i != e; i++) |
| 524 | { |
| 525 | if(path[i] == '.') |
| 526 | path[i] = '/'; |
| 527 | } |
| 528 | SafeSprintf(cPath, 256 - int(cPath - path), ".nc"); |
| 529 | const char *bytecode = BinaryCache::GetBytecode(path); |
| 530 | if(!bytecode && importPath) |
| 531 | bytecode = BinaryCache::GetBytecode(pathNoImport); |
| 532 | |
| 533 | // Create module if not found |
| 534 | if(!bytecode) |
| 535 | bytecode = BuildModule(path, pathNoImport); |
| 536 | if(!bytecode) |
| 537 | { |
| 538 | SafeSprintf(errBuf, 256, "Failed to build module %s", module); |
| 539 | CodeInfo::lastError.Init(errBuf, NULL); |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | unsigned int hash = GetStringHash(name); |
| 544 | ByteCode *code = (ByteCode*)bytecode; |
| 545 | |
| 546 | // Find function and set pointer |
| 547 | ExternFuncInfo *fInfo = FindFirstFunc(code); |
| 548 | |
| 549 | unsigned int end = code->functionCount - code->moduleFunctionCount; |
| 550 | for(unsigned int i = 0; i < end; i++) |
| 551 | { |
| 552 | if(hash == fInfo->nameHash) |
| 553 | { |
| 554 | if(index == 0) |
| 555 | { |
| 556 | fInfo->address = -1; |
| 557 | fInfo->funcPtr = (void*)ptr; |
| 558 | index--; |
| 559 | break; |
| 560 | } |
| 561 | index--; |
| 562 | } |
| 563 | fInfo++; |
| 564 | } |
| 565 | if(index != -1) |
| 566 | { |
| 567 | SafeSprintf(errBuf, 256, "ERROR: function %s or one of it's overload is not found in module %s", name, module); |
| 568 | CodeInfo::lastError.Init(errBuf, NULL); |
| 569 | return false; |
no test coverage detected