Repository navigation
CSEC: Native methods #78
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdb5402
native methods
mattcce 7cf369d
display native function descriptor when unimplemented
mattcce c695529
native method handling for type-checker
mattcce 86a2b1d
revert Object class declaration
mattcce 1734ed5
Handle bodyless methods in enums; drop type checker console.log
hj235 3b3abbb
Run native handler control items; reject native methods with a body
hj235 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { natives } from '../natives' | ||
| import { | ||
| // ControlStub, | ||
| // StashStub, | ||
| createContextStub | ||
| // getControlItemStr, | ||
| // getStashItemStr | ||
| } from './__utils__/utils' | ||
| import { parse } from '../../ast/parser' | ||
| import { evaluate } from '../interpreter' | ||
|
|
||
| describe('native functions', () => { | ||
| it('should invoke external native function', () => { | ||
| const mockForeignFn = jest.fn() | ||
|
|
||
| natives['testNative(): void'] = mockForeignFn | ||
|
|
||
| const programStr = ` | ||
| class C { | ||
| public native void testNative(); | ||
|
|
||
| public static void main(String[] args) { | ||
| C c = new C(); | ||
| c.testNative(); | ||
| } | ||
| }` | ||
|
|
||
| const compilationUnit = parse(programStr) | ||
| expect(compilationUnit).toBeTruthy() | ||
|
|
||
| const context = createContextStub() | ||
| context.control.push(compilationUnit!) | ||
|
|
||
| evaluate(context) | ||
|
|
||
| expect(mockForeignFn.mock.calls).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('should invoke external native function with correct environment', () => { | ||
| const foreignFn = jest.fn(({ environment }) => { | ||
| const s = environment.getVariable('s').value.literalType.value | ||
| expect(s).toBe('"Test"') | ||
| }) | ||
|
|
||
| natives['testNative(String s): void'] = foreignFn | ||
|
|
||
| const programStr = ` | ||
| class C { | ||
| public native void testNative(String s); | ||
|
|
||
| public static void main(String[] args) { | ||
| C c = new C(); | ||
| c.testNative("Test"); | ||
| } | ||
| }` | ||
|
|
||
| const compilationUnit = parse(programStr) | ||
| expect(compilationUnit).toBeTruthy() | ||
|
|
||
| const context = createContextStub() | ||
| context.control.push(compilationUnit!) | ||
|
|
||
| evaluate(context) | ||
|
|
||
| expect(foreignFn.mock.calls).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('should run control items pushed by external native function', () => { | ||
| const foreignFn = jest.fn(({ control }) => { | ||
| control.push({ kind: 'Literal', literalType: { kind: 'DecimalIntegerLiteral', value: '7' } }) | ||
| }) | ||
|
|
||
| const checkFn = jest.fn(({ environment }) => { | ||
| const x = environment.getVariable('x').value.literalType.value | ||
| expect(x).toBe('7') | ||
| }) | ||
|
|
||
| natives['viaControl(): int'] = foreignFn | ||
| natives['check(int x): void'] = checkFn | ||
|
|
||
| const programStr = ` | ||
| class C { | ||
| public static native int viaControl(); | ||
| public static native void check(int x); | ||
|
|
||
| public static void main(String[] args) { | ||
| int x = viaControl(); | ||
| check(x); | ||
| } | ||
| }` | ||
|
|
||
| const compilationUnit = parse(programStr) | ||
| expect(compilationUnit).toBeTruthy() | ||
|
|
||
| const context = createContextStub() | ||
| context.control.push(compilationUnit!) | ||
|
|
||
| evaluate(context) | ||
|
|
||
| expect(foreignFn.mock.calls).toHaveLength(1) | ||
| expect(checkFn.mock.calls).toHaveLength(1) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Control, Environment, Stash } from './components' | ||
|
|
||
| /* | ||
| Native function escape hatch. | ||
|
|
||
| Used for implementing native methods. Allows for purely arbitrary modification to the control, stash, and environment via an external handler function. | ||
|
|
||
| All native functions are expected to respect Java method call preconditions and postconditions, with the exception of returning. When a native function is called, it can expect the following. | ||
|
|
||
| Preconditions: environment has been initialised for the current function call. | ||
|
|
||
| Postconditions: returned result must be pushed onto the top of the stash. | ||
|
|
||
| The current implementation automatically injects a return instruction after the external handler function call ends. | ||
| */ | ||
|
|
||
| export type NativeFunction = ({ | ||
| control, | ||
| stash, | ||
| environment | ||
| }: { | ||
| control: Control | ||
| stash: Stash | ||
| environment: Environment | ||
| }) => void | ||
|
|
||
| export const natives: { | ||
| [descriptor: string]: NativeFunction | ||
| } = {} | ||
|
hj235 marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.