Last active
June 21, 2022 09:39
-
-
Save Luna-Klatzer/23c094059924210db24397328b7fe8a8 to your computer and use it in GitHub Desktop.
Simple proof of concept for a possible implementation modules in Kipper, which would be usable in a Web API that supports running Kipper files like in the WASM API.
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
// -------------------------------- | |
// file1.ts | |
// -------------------------------- | |
// Kipper Built-ins | |
function __print__(msg: string): void { console.log(msg); } | |
// Import other Kipper modules | |
import * as __file2__ from "./file2"; | |
type moduleExports = Record<string, any>; | |
type kipperModule = { fileName: string, exports: moduleExports }; | |
// Local imports | |
const __imports__: moduleExports = {}; | |
// Local exports | |
const __module__: kipperModule = { | |
fileName: __filename, | |
exports: {} | |
}; | |
let __loaded__: boolean = false; | |
// Load function for the module | |
export const __loadKipperModule__: () => kipperModule = () => { | |
// Avoid loading a module again | |
if (__loaded__) { | |
return __module__; | |
} | |
function helloWorld(): void { | |
__print__("Hello world from 'file1.ts'"); | |
} | |
__module__.exports.helloWorld = helloWorld; | |
// Wrap-up load of module | |
__loaded__ = true; | |
return __module__; | |
}; | |
// -------------------------------- | |
// file2.ts | |
// -------------------------------- | |
// Kipper Built-ins | |
function __print__(msg: string): void { console.log(msg); } | |
// Import other Kipper modules | |
import * as __file1__ from "./file1"; | |
type moduleExports = Record<string, any>; | |
type kipperModule = { fileName: string, exports: moduleExports }; | |
// Local imports | |
const __imports__: moduleExports = {}; | |
// Local exports | |
const __module__: kipperModule = { | |
fileName: __filename, | |
exports: {} | |
}; | |
let __loaded__: boolean = false; | |
// Load function for the module | |
export const __loadKipperModule__: () => kipperModule = () => { | |
// Avoid loading a module again | |
if (__loaded__) { | |
return __module__; | |
} | |
// Run code as done in the Kipper file | |
__imports__["file1"] = __file1__.__loadKipperModule__(); | |
// Hello world from 'file1' | |
__imports__["file1"]["exports"].helloWorld(); | |
function helloWorld(): void { | |
__print__("Hello world from 'file2.ts'"); | |
} | |
__module__.exports.helloWorld = helloWorld; | |
// Wrap-up load of module | |
__loaded__ = true; | |
return __module__; | |
}; | |
// -------------------------------- | |
// main.ts | |
// -------------------------------- | |
// Kipper Built-ins | |
function __print__(msg: string): void { console.log(msg); } | |
// Import other Kipper modules | |
import * as __file1__ from "./file1"; | |
import * as __file2__ from "./file2"; | |
type moduleExports = Record<string, any>; | |
type kipperModule = { fileName: string, exports: moduleExports }; | |
// Local imports | |
const __imports__: moduleExports = {}; | |
// Local exports | |
const __module__: kipperModule = { | |
fileName: __filename, | |
exports: {} | |
}; | |
let __loaded__: boolean = false; | |
// Load function for the module | |
export const __loadKipperModule__: () => kipperModule = () => { | |
// Avoid loading a module again | |
if (__loaded__) { | |
return __module__; | |
} | |
// Run code as done in the Kipper file | |
__imports__["file1"] = __file1__.__loadKipperModule__(); | |
__imports__["file2"] = __file2__.__loadKipperModule__(); | |
function helloWorld(): void { | |
// Hello world from 'file1' | |
__imports__["file1"]["exports"].helloWorld(); | |
// Hello world from 'file2' | |
__imports__["file2"]["exports"].helloWorld(); | |
__print__("Hello world from 'main.ts'"); | |
} | |
__module__.exports.helloWorld = helloWorld; | |
helloWorld(); | |
// Wrap-up load of module | |
__loaded__ = true; | |
return __module__; | |
}; | |
// Entry-script, run '__loadKipperModule__' when the file is called | |
__loadKipperModule__(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment