Skip to content

Native methods for CSEC machine #74

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ node_modules
dist

src/ast
src/ec-evaluator
62 changes: 31 additions & 31 deletions src/ec-evaluator/__tests__/__utils__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { Environment } from "../../components";
import { STEP_LIMIT } from "../../constants";
import { ControlItem, Context, StashItem, StructType } from "../../types";
import { Stack, isNode } from "../../utils";
import { Environment } from '../../components'
import { STEP_LIMIT } from '../../constants'
import { ControlItem, Context, StashItem, StructType } from '../../types'
import { Stack, isNode } from '../../utils'

export class StackStub<T> extends Stack<T> {
private trace: T[] = [];
private trace: T[] = []

public push(...items: T[]): void {
for (const item of items) {
super.push(item);
this.trace.push(item);
super.push(item)
this.trace.push(item)
}
};
}

public getTrace(): T[] {
return this.trace;
};
};
export class ControlStub extends StackStub<ControlItem> {};
export class StashStub extends StackStub<StashItem> {};
return this.trace
}
}
export class ControlStub extends StackStub<ControlItem> {}
export class StashStub extends StackStub<StashItem> {}
// TODO make env traceable
export class EnvironmentStub extends Environment {};
export class EnvironmentStub extends Environment {}

export const createContextStub = (): Context => ({
errors: [],
control: new ControlStub(),
stash: new StashStub(),
environment: new EnvironmentStub(),
totalSteps: STEP_LIMIT,
});
totalSteps: STEP_LIMIT
})

export const getControlItemStr = (i: ControlItem): string => {
return isNode(i) ? i.kind : i.instrType;
};
return isNode(i) ? i.kind : i.instrType
}

export const getStashItemStr = (i: StashItem): string => {
return i.kind === "Literal"
? i.literalType.value
return i.kind === 'Literal'
? i.literalType.value
: i.kind === StructType.CLOSURE
? i.mtdOrCon.kind === "MethodDeclaration"
? i.mtdOrCon.methodHeader.identifier
: i.mtdOrCon.constructorDeclarator.identifier
: i.kind === StructType.VARIABLE
? i.name
: i.kind === StructType.CLASS
? i.frame.name
: i.kind === StructType.TYPE
? i.type
: i.kind;
};
? i.decl.kind === 'MethodDeclaration' || i.decl.kind === 'NativeDeclaration'
? i.decl.methodHeader.identifier
: i.decl.constructorDeclarator.identifier
: i.kind === StructType.VARIABLE
? i.name
: i.kind === StructType.CLASS
? i.frame.name
: i.kind === StructType.TYPE
? i.type
: i.kind
}
95 changes: 95 additions & 0 deletions src/ec-evaluator/__tests__/natives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { foreigns } from '../natives'
import {
// ControlStub,
// StashStub,
createContextStub
// getControlItemStr,
// getStashItemStr
} from './__utils__/utils'
import { parse } from '../../ast/parser'
import { evaluate } from '../interpreter'

describe('native functions', () => {
it('method declaration should dynamically link foreign function', () => {
const mockForeignFn = jest.fn()

foreigns['C::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)

const c = context.environment.getClass('C')
c.instanceMethods.find(md => md)
})

it('invoke instruction should invoke external native function', () => {
const mockForeignFn = jest.fn()

foreigns['C::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"')
})

foreigns['C::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)
})
})
Loading