Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error and stop build a contract that has method name confliction #346

Merged
merged 3 commits into from
Feb 24, 2023

Conversation

ailisp
Copy link
Member

@ailisp ailisp commented Feb 23, 2023

This PR is to address #121.

The near-sdk-js builds a contract by generate a C file and turns each contract method to a C function, then compile this C file into a wasm file. Therefore, it's possible the contract method name conflicts with the C function name in builder.c.

Because a contract method name foo will generate a void foo(void) in the C file, there're totally 4 possible cases of different result will happen. Those are a contract method name foo becomes the same as:

  1. a void foo(void) in builder.c or it's include
  2. an extern void foo(void) in builder.cor it's include
  3. a foo function with other signature in builder.c or it's include. E.g.: strcpy
  4. an extern foo function with other signature. E.g.: storage_read.

Case 1. doesn't exist as we checked builder.c and it's include. Case 2. only has one function panic. If user attempts to define a panic contract method, it will cause env.panic() call that method instead, which causes misleading result. In this PR this becomes an error. In case 3 and 4, the C -> WASM step C compiler gives error because breaks the C signature. In this PR, it collects the error message, transform it into a user friendly one and inform user to avoid using foo and ends the build.

All cases are tested with the CLI in this PR. It reports errors like this:

  • case 2:

image

- case 3&4:

image

Copy link
Collaborator

@volovyks volovyks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good explanation and simple solution, nice work!

@ailisp ailisp merged commit 4b7fc3c into develop Feb 24, 2023
@ailisp ailisp deleted the fix-contract-method-conflicting branch February 24, 2023 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants