Skip to content

Commit

Permalink
safe evaluate code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 8, 2024
1 parent b75d98e commit 11087e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
9 changes: 4 additions & 5 deletions lib/code-runner/run-prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ export const runInitialPrompt = async (
syntaxError,
circuitErrors,
typescriptErrors,
} = safeEvaluateCode(
codefence,
context.outputType,
context.preSuppliedImports,
)
} = safeEvaluateCode(codefence, {
outputType: context.outputType,
preSuppliedImports: context.preSuppliedImports,
})

if (success) {
return {
Expand Down
14 changes: 8 additions & 6 deletions lib/code-runner/safe-evaluate-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as React from "react"
import { Circuit } from "@tscircuit/core"
import { safeTranspileCode } from "./transpile-code"
import Debug from "debug"
import type { CodeRunnerContext } from "./code-runner-context"

const debug = Debug("tscircuit:prompt")

export const safeEvaluateCode = (
code: string,
type: "board" | "footprint" | "package" | "model" = "board",
preSuppliedImports: Record<string, any> = {},
context: CodeRunnerContext,
):
| {
success: true
Expand All @@ -35,6 +35,8 @@ export const safeEvaluateCode = (
} => {
globalThis.React = React

const { preSuppliedImports = {}, outputType } = context

// Add pre-supplied imports to the global scope
for (const [key, value] of Object.entries(preSuppliedImports)) {
;(globalThis as any)[key] = value
Expand Down Expand Up @@ -118,21 +120,21 @@ export const safeEvaluateCode = (

const circuit = new Circuit()
try {
if (type === "board") {
if (outputType === "board") {
circuit.add(<UserElm />)
} else if (type === "package") {
} else if (outputType === "package") {
circuit.add(
<board width="10mm" height="10mm">
<UserElm name="U1" />
</board>,
)
} else if (type === "footprint") {
} else if (outputType === "footprint") {
circuit.add(
<board width="10mm" height="10mm">
<chip name="U1" footprint={<UserElm />} />
</board>,
)
} else if (type === "model") {
} else if (outputType === "model") {
// Note: This part might need adjustment as we don't have access to jscad-related imports
// const jscadGeoms: any[] = []
// const { createJSCADRoot } = createJSCADRenderer(jscadPlanner as any)
Expand Down
7 changes: 5 additions & 2 deletions tests/lib/safe-evaluate-code.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export default function TestComponent() {
}
`

const result = safeEvaluateCode(testCode, "board", {
"@tsci/seveibar.custom-component": MyCustomComponent,
const result = safeEvaluateCode(testCode, {
outputType: "board",
preSuppliedImports: {
"@tsci/seveibar.custom-component": MyCustomComponent,
},
})

expect(result.success).toBe(true)
Expand Down

0 comments on commit 11087e8

Please sign in to comment.