Skip to content

Commit a3057b0

Browse files
committed
ZOMG
1 parent 0df0acf commit a3057b0

File tree

6 files changed

+145
-18
lines changed

6 files changed

+145
-18
lines changed

app/javascript/components/bootcamp/SolveExercisePage/hooks/useConstructRunCode/useConstructRunCode.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ export function useConstructRunCode({
9494
.forEach((e) => e.remove())
9595

9696
// @ts-ignore
97-
const compiled = compile(studentCode, {
98-
languageFeatures: config.interpreterOptions,
99-
customFunctions: customFunctionsForInterpreter.map((cfn) => {
100-
return { name: cfn.name, arity: cfn.arity, code: cfn.code }
101-
}),
102-
})
103-
104-
const error = compiled.error as CompilationError
105-
106-
if (error) {
107-
handleCompilationError(error, editorView)
108-
return
109-
}
97+
// const compiled = compile(studentCode, {
98+
// languageFeatures: config.interpreterOptions,
99+
// customFunctions: customFunctionsForInterpreter.map((cfn) => {
100+
// return { name: cfn.name, arity: cfn.arity, code: cfn.code }
101+
// }),
102+
// })
103+
104+
// const error = compiled.error as CompilationError
105+
106+
// if (error) {
107+
// handleCompilationError(error, editorView)
108+
// return
109+
// }
110110

111111
let testResults
112112

app/javascript/components/bootcamp/SolveExercisePage/test-runner/generateAndRunTestSuite/execTest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
AnimationTimeline,
1212
} from '../../AnimationTimeline/AnimationTimeline'
1313
import { Frame } from '@/interpreter/frames'
14+
import { execJS } from '../../../../execJS'
1415

1516
/**
1617
This is of type TestCallback
@@ -32,7 +33,8 @@ export function execTest(
3233

3334
const args = testData.args ? parseArgs(testData.args) : []
3435

35-
let evaluated
36+
let evaluated = execJS(options.studentCode)
37+
/*let evaluated
3638
if (testData.function) {
3739
evaluated = evaluateFunction(
3840
options.studentCode,
@@ -42,7 +44,7 @@ export function execTest(
4244
)
4345
} else {
4446
evaluated = interpret(options.studentCode, context)
45-
}
47+
}*/
4648

4749
const { value: actual, frames } = evaluated
4850

app/javascript/components/execJS.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { generate } from 'astring'
2+
import { parse } from 'acorn'
3+
import { instrument } from 'aran'
4+
import { Frame } from '@/interpreter/frames'
5+
6+
export const execJS = (code: string) => {
7+
return runCode(code)
8+
}
9+
10+
function isNumeric(n) {
11+
return !isNaN(parseFloat(n)) && isFinite(n)
12+
}
13+
14+
const runCode = (code: string) => {
15+
const parsedCode = parse(code, {
16+
sourceType: 'script',
17+
locations: true,
18+
ecmaVersion: 2024,
19+
})
20+
21+
const extactLOCFromTag = (tag) => {
22+
let node = parsedCode.body
23+
tag
24+
.replace('main#$.body.', '')
25+
.split('.')
26+
.forEach((elem) => {
27+
if (isNumeric(elem)) {
28+
node = node[parseInt(elem)]
29+
} else {
30+
node = node[elem]
31+
}
32+
})
33+
return node.loc
34+
}
35+
36+
const advice_global_variable = '_ARAN_ADVICE_'
37+
const data_global_variable = '_DATA_'
38+
39+
let data = { time: 0 }
40+
const frames: Frame[] = []
41+
const describeFrame = (value) => {
42+
if (typeof value === 'function') {
43+
return String(value.name || 'anonynmous')
44+
} else if (typeof value === 'object' && value !== null) {
45+
return '#' + Object.prototype.toString.call(value).slice(8, -1)
46+
} else if (typeof value === 'symbol') {
47+
return '@' + String(value.description ?? 'unknown')
48+
} else if (typeof value === 'string') {
49+
return JSON.stringify(value)
50+
} else {
51+
return String(value)
52+
}
53+
}
54+
const addFrame = (
55+
code: string,
56+
status: 'SUCCESS' | 'ERROR',
57+
callee: any,
58+
location: any
59+
) => {
60+
const time = globalThis._DATA_.time
61+
const line = extactLOCFromTag(location).start.line
62+
const frame: Frame = {
63+
timelineTime: time * 100,
64+
time: globalThis._DATA_.time,
65+
line: line,
66+
code: code,
67+
status: status,
68+
69+
description: () => {
70+
return describeFrame(callee)
71+
},
72+
}
73+
frames.push(frame)
74+
globalThis._DATA_.time += 0.01
75+
}
76+
globalThis._ADD_FRAME_ = addFrame
77+
78+
const advice = {
79+
'apply@around': (_state, callee, that, input, location) => {
80+
// console.log(_state, callee, that, input, location)
81+
const result = Reflect.apply(callee, that, input)
82+
globalThis._ADD_FRAME_('', 'SUCCESS', callee, location)
83+
return result
84+
},
85+
}
86+
87+
Reflect.defineProperty(globalThis, advice_global_variable, { value: advice })
88+
Reflect.defineProperty(globalThis, data_global_variable, { value: {} })
89+
90+
// Reset time
91+
globalThis._DATA_.time = 0
92+
93+
const root2 = instrument(
94+
{ kind: 'eval', path: 'main', root: parsedCode },
95+
{ mode: 'standalone', advice_global_variable, pointcut: ['apply@around'] }
96+
)
97+
const res = globalThis.eval(generate(root2))
98+
console.log(res)
99+
return { value: res, frames: frames }
100+
}

app/javascript/interpreter/frames.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export type Frame = {
4747
code: string
4848
status: FrameExecutionStatus
4949
error?: RuntimeError
50-
priorVariables: Record<string, any>
51-
variables: Record<string, any>
50+
// priorVariables: Record<string, any>
51+
variables?: Record<string, any>
5252
time: number
5353
timelineTime: number
5454
result?: EvaluationResult

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@gleam-lang/highlight.js-gleam": "^1.0.0",
3434
"@hotwired/stimulus": "^3.2.2",
3535
"@hotwired/turbo-rails": "^7.3.0",
36+
"@juliangarnierorg/anime-beta": "^4.0.0-rc.4",
3637
"@plutojl/lang-julia": "^0.12.1",
3738
"@popperjs/core": "^2.11.8",
3839
"@rails/actioncable": "^6.0.0",
@@ -46,8 +47,10 @@
4647
"@xstate/react": "^3.2.2",
4748
"abortcontroller-polyfill": "^1.7.3",
4849
"ace-builds": "^1.4.12",
50+
"acorn": "^8.14.1",
4951
"actioncable": "^5.2.4-3",
50-
"@juliangarnierorg/anime-beta": "^4.0.0-rc.4",
52+
"aran": "^5.1.0",
53+
"astring": "^1.9.0",
5154
"autoprefixer": "latest",
5255
"browserslist-to-esbuild": "^1.1.1",
5356
"canvas-confetti": "^1.9.3",

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,11 @@ acorn@^7.1.1, acorn@^7.4.0:
26312631
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.4.1.tgz#feaed255973d2e77555b83dbc08851a6c63520fa"
26322632
integrity sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==
26332633

2634+
acorn@^8.14.1:
2635+
version "8.14.1"
2636+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.1.tgz#721d5dc10f7d5b5609a891773d47731796935dfb"
2637+
integrity sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg==
2638+
26342639
acorn@^8.2.4:
26352640
version "8.14.0"
26362641
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0"
@@ -2798,6 +2803,13 @@ aproba@^1.0.3, aproba@^1.1.1, aproba@^1.1.2:
27982803
resolved "https://registry.yarnpkg.com/aproba/-/aproba-2.0.0.tgz#52520b8ae5b569215b354efc0caa3fe1e45a8adc"
27992804
integrity sha512-lYe4Gx7QT+MKGbDsA+Z+he/Wtef0BiwDOlK/XkBrdfsh9J/jPPXbX0tE9x9cl27Tmu5gg3QUbUrQYa/y+KOHPQ==
28002805

2806+
aran@^5.1.0:
2807+
version "5.1.0"
2808+
resolved "https://registry.yarnpkg.com/aran/-/aran-5.1.0.tgz#e8583510d5b197c7df1cbbbb5553b41eefaa6b5c"
2809+
integrity sha512-km8oG8EMRtn1MPnBJ7upMS4ikLrmH3ja75+anzVod8qj2hCrYcdXl5l5VsAEMY5ZqoT0WrPrPntsG4QzH2vRvA==
2810+
dependencies:
2811+
estree-sentry "^0.4.1"
2812+
28012813
archy@~1.0.0:
28022814
version "1.0.0"
28032815
resolved "https://registry.yarnpkg.com/archy/-/archy-1.0.0.tgz#f9c8c13757cc1dd7bc379ac77b2c62a5c2868c40"
@@ -2991,6 +3003,11 @@ astral-regex@^2.0.0:
29913003
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
29923004
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==
29933005

3006+
astring@^1.9.0:
3007+
version "1.9.0"
3008+
resolved "https://registry.yarnpkg.com/astring/-/astring-1.9.0.tgz#cc73e6062a7eb03e7d19c22d8b0b3451fd9bfeef"
3009+
integrity sha512-LElXdjswlqjWrPpJFg1Fx4wpkOCxj1TDHlSV4PlaRxHGWko024xICaa97ZkMfs6DRKlCguiAI+rbXv5GWwXIkg==
3010+
29943011
asynckit@^0.4.0:
29953012
version "0.4.0"
29963013
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
@@ -5023,6 +5040,11 @@ estraverse@^5.1.0, estraverse@^5.2.0, estraverse@^5.3.0:
50235040
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.3.0.tgz#2eea5290702f26ab8fe5370370ff86c965d21123"
50245041
integrity sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==
50255042

5043+
estree-sentry@^0.4.1:
5044+
version "0.4.1"
5045+
resolved "https://registry.yarnpkg.com/estree-sentry/-/estree-sentry-0.4.1.tgz#7f258ae702d54c04f17712431bd8bb0d3f5e7f10"
5046+
integrity sha512-tTlF1mwCLlKf0dWMc9Xi9Ady3d6I1cq/hCTsMPZF6EmfJjr9gpewJqOPLKJEhxHYPY6dzzMPO2w3qS8mbmNVLQ==
5047+
50265048
esutils@^2.0.2:
50275049
version "2.0.3"
50285050
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"

0 commit comments

Comments
 (0)