Created
June 16, 2024 13:33
-
-
Save MarkTiedemann/025f89ad4ae1a4f595830cc58ff417ac to your computer and use it in GitHub Desktop.
Run ABAP in Node
This file contains 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
CLASS main DEFINITION | |
PUBLIC | |
FINAL | |
CREATE PUBLIC. | |
PUBLIC SECTION. | |
METHODS say_hello | |
IMPORTING | |
name TYPE string | |
RETURNING | |
VALUE(result) TYPE string. | |
METHODS run. | |
PROTECTED SECTION. | |
PRIVATE SECTION. | |
ENDCLASS. | |
CLASS main IMPLEMENTATION. | |
METHOD say_hello. | |
result = |Hello, { name }!|. | |
ENDMETHOD. | |
METHOD run. | |
WRITE say_hello( `World` ). | |
ENDMETHOD. | |
ENDCLASS. |
This file contains 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
{ | |
"devDependencies": { | |
"@abaplint/runtime": "^2.8.27", | |
"@abaplint/transpiler": "^2.8.27" | |
} | |
} |
This file contains 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
import { readFile } from "node:fs/promises"; | |
import { createContext, runInContext } from "node:vm"; | |
import { ABAP, MemoryConsole } from "@abaplint/runtime"; | |
import { Transpiler } from "@abaplint/transpiler"; | |
const filename = "main.prog.abap"; | |
const contents = await readFile(filename, "utf-8"); | |
const transpiler = new Transpiler(); | |
const result = await transpiler.runRaw([{ filename, contents }]); | |
const code = result.objects[0].chunk.getCode(); | |
const abap = new ABAP({ console: new MemoryConsole() }); | |
const context = { abap }; | |
createContext(context); | |
runInContext(code, context); | |
const main = new context.abap.Classes["MAIN"](); | |
await main.run(); | |
console.log(context.abap.console.get()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment