Repository navigation
CSEC: Native methods - #78
Conversation
Coverage report
Show new covered files 🐣
Show files with reduced coverage 🔻
Test suite run success1202 tests passing in 71 suites. Report generated by 🧪jest coverage report action from 3b3abbb |
6b33b64 to
0103daf
Compare
51e6a47 to
86a2b1d
Compare
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 86a2b1d164
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Address the Codex review of this PR: - A native method declared in an enum crashed typeCheck, because the extractor now yields an undefined body for `;` and only the class branch of the checker handled that. Give the EnumDeclaration branch the same native / MissingMethodBodyError handling. - Remove the leftover console.log of native method declarations.
|
Rebased onto On top of that, one commit addressing the Codex review: removes the stray Build is clean and the full suite passes (1199 passed, 6 skipped), including What this adds
Not addressed here, deliberately:
|
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1734ed5818
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Address the second Codex review: - Push the reset instruction before calling the native handler, so control items the handler pushes run instead of being discarded by RESET. - Reject native methods that declare a body, in classes and enums, with NativeMethodHasBodyError (as in #79).
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Introduction
Native methods in Java provide FFI functionality to call foreign functions. Native methods are especially useful in the CSE machine to implement primitive functions, and in some cases, are the only real way to do so.
Motivation
The motivation for implementing native methods initially stemmed from
Object.hashCode, which itself is a native method.Native methods have clear uses for some other cases as well, like with displaying or printing to standard output. Other specific uses for native methods are presently unclear, but having the ability to break out of the CSEC machine is certainly not a detriment.
Implementation
Notation note: we refer generally to a Java syntactic native method by the term native method, and the foreign function that is eventually called when the native method is invoked by the term foreign function.
CSEC Handling
Two control items (labelled C1, C2) have been modified to allow for native methods to be properly handled.
(C1) Method Declarations (AST Node
MethodDeclaration) will now dynamically link a foreign function to a native method declaration. Closures (theClosuretype) now distinguishes between three types of declarations (previously two), with the newest beingNativeDeclaration.Constructors cannot be native in Java.
NativeDeclarationsmirror theMethodDeclarationtype, except it does not have a method body but instead hold a pointer to the resolved foreign function.Implementation note: even though
MethodDeclarationandConstructorDeclaration, both of which are contained within theClosuretype, are defined in the AST (inast/types/classes.ts), we instead choose to defineNativeDeclarationinec-evaluator/types.tsinstead, to localise these changes.(C2) Invocations (Instruction
invoke) will now distinguish the newClosuretype, and handle it specially. In particular, retains most of the usual functionality, and will appropriately initialise a new environment frame for the function to be executed (see native method architecture below), and push areturninstruction before directly calling the foreign function with the full CSEC machine state.Native Method Architecture
We offer full and complete power to native methods, passing in the entire control, stash, and environment to foreign functions they call. This matches the power that foreign functions are expected to have.
The CSEC machine still handles argument resolution and returning. This is because FFI calls should function like normal functions to the CSEC machine, except that it ignores whatever happens while the foreign function is running. To this end, common foreign functions are expected to:
Note that the foreign function, using common utility methods that the rest of the CSEC machine uses, will have to appropriately deserialise and serialise the values. In particular, it must be able to destructure AST nodes that it expects and reconstruct valid AST nodes that the CSEC machine expects.
Because the full control, stash, and environment is passed to the foreign function, this allows foreign functions to effectively do anything to the CSEC machine state. However, foreign functions are generally not permitted to expect anything except its arguments being defined in the current environment.
The dictionary used uses fully-qualified method descriptors to identify the correct foreign function. These are of the form:
Note that foreign functions always have the same type:
In particular, foreign functions always return nothing. Their results are to be injected directly into the stash that is provided, for use by the CSEC machine (and the program it is currently running).
The identifier is included in such a symbolic reference for the express purpose of making foreign functions easier to implement. Because foreign functions must retrieve their arguments from the environment, their implementations must be aware of the correct parameter identifiers.
Notes: