Skip to content

Commit

Permalink
Reduces subset of JSDoc used, changes typing for CreateEngineOptions,…
Browse files Browse the repository at this point in the history
… original method of extracting from the constructor gave any types on the options, this adds explicit typing and documents 2 options
  • Loading branch information
PlaryWasTaken committed Feb 5, 2024
1 parent 9a121c5 commit 687f994
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
20 changes: 10 additions & 10 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CreateEngineOptions } from './types'
import Global from './global'
import Thread from './thread'
import createErrorType from './type-extensions/error'
Expand All @@ -20,7 +21,7 @@ export default class LuaEngine {
enableProxy = true,
traceAllocations = false,
functionTimeout = undefined as number | undefined,
} = {},
}: CreateEngineOptions = {},
) {
this.global = new Global(this.cmodule, traceAllocations)

Expand Down Expand Up @@ -57,9 +58,8 @@ export default class LuaEngine {

/**
* Executes Lua code from a string asynchronously.
* @async
* @param {string} script - Lua script to execute.
* @returns {Promise<any>} - A Promise that resolves to the result returned by the Lua script execution.
* @param script - Lua script to execute.
* @returns A Promise that resolves to the result returned by the Lua script execution.
* @throws {Error}
*/
public doString(script: string): Promise<any> {
Expand All @@ -69,8 +69,8 @@ export default class LuaEngine {
/**
* Executes Lua code from a file asynchronously.
* @async
* @param {string} filename - Path to the Lua script file.
* @returns {Promise<any>} - A Promise that resolves to the result returned by the Lua script execution.
* @param filename - Path to the Lua script file.
* @returns - A Promise that resolves to the result returned by the Lua script execution.
* @throws {Error}
*/
public doFile(filename: string): Promise<any> {
Expand All @@ -79,8 +79,8 @@ export default class LuaEngine {

/**
* Executes Lua code from a string synchronously.
* @param {string} script - Lua script to execute.
* @returns {any} - The result returned by the Lua script.
* @param script - Lua script to execute.
* @returns - The result returned by the Lua script.
* @throws {Error}
*/
public doStringSync(script: string): any {
Expand All @@ -91,8 +91,8 @@ export default class LuaEngine {

/**
* Executes Lua code from a file synchronously.
* @param {string} filename - Path to the Lua script file.
* @returns {any} - The result returned by the Lua script.
* @param filename - Path to the Lua script file.
* @returns - The result returned by the Lua script.
* @throws {Error}
*/
public doFileSync(filename: string): any {
Expand Down
28 changes: 11 additions & 17 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import version from 'package-version'

/**
* Represents a factory for creating and configuring Lua engines.
* @class
* @export
*/
export default class LuaFactory {
/**
Expand All @@ -18,9 +16,8 @@ export default class LuaFactory {

/**
* Constructs a new LuaFactory instance.
* @constructor
* @param {string} [customWasmUri] - Custom URI for the Lua WebAssembly module.
* @param {EnvironmentVariables} [environmentVariables] - Environment variables for the Lua engine.
* @param [customWasmUri] - Custom URI for the Lua WebAssembly module.
* @param [environmentVariables] - Environment variables for the Lua engine.
*/
public constructor(customWasmUri?: string, environmentVariables?: EnvironmentVariables) {
if (customWasmUri === undefined) {
Expand All @@ -38,20 +35,19 @@ export default class LuaFactory {

/**
* Mounts a file in the Lua environment asynchronously.
* @async
* @param {string} path - Path to the file in the Lua environment.
* @param {string | ArrayBufferView} content - Content of the file to be mounted.
* @returns {Promise<void>} - A Promise that resolves once the file is mounted.
* @param path - Path to the file in the Lua environment.
* @param content - Content of the file to be mounted.
* @returns - A Promise that resolves once the file is mounted.
*/
public async mountFile(path: string, content: string | ArrayBufferView): Promise<void> {
this.mountFileSync(await this.getLuaModule(), path, content)
}

/**
* Mounts a file in the Lua environment synchronously.
* @param {LuaWasm} luaWasm - Lua WebAssembly module.
* @param {string} path - Path to the file in the Lua environment.
* @param {string | ArrayBufferView} content - Content of the file to be mounted.
* @param luaWasm - Lua WebAssembly module.
* @param path - Path to the file in the Lua environment.
* @param content - Content of the file to be mounted.
*/
public mountFileSync(luaWasm: LuaWasm, path: string, content: string | ArrayBufferView): void {
const fileSep = path.lastIndexOf('/')
Expand Down Expand Up @@ -84,18 +80,16 @@ export default class LuaFactory {

/**
* Creates a Lua engine with the specified options.
* @async
* @param {CreateEngineOptions} [options] - Configuration options for the Lua engine.
* @returns {Promise<LuaEngine>} - A Promise that resolves to a new LuaEngine instance.
* @param [options] - Configuration options for the Lua engine.
* @returns - A Promise that resolves to a new LuaEngine instance.
*/
public async createEngine(options: CreateEngineOptions = {}): Promise<LuaEngine> {
return new LuaEngine(await this.getLuaModule(), options)
}

/**
* Gets the Lua WebAssembly module.
* @async
* @returns {Promise<LuaWasm>} - A Promise that resolves to the Lua WebAssembly module.
* @returns - A Promise that resolves to the Lua WebAssembly module.
*/
public async getLuaModule(): Promise<LuaWasm> {
return this.luaWasmPromise
Expand Down
29 changes: 13 additions & 16 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ interface LuaMemoryStats {

/**
* Represents the global state of the Lua engine.
* @class
* @export
*/
export default class Global extends Thread {
private memoryStats: LuaMemoryStats | undefined
private allocatorFunctionPointer: number | undefined

/**
* Constructs a new Global instance.
* @constructor
* @param {LuaWasm} cmodule - The Lua WebAssembly module.
* @param {boolean} shouldTraceAllocations - Whether to trace memory allocations.
* @param cmodule - The Lua WebAssembly module.
* @param shouldTraceAllocations - Whether to trace memory allocations.
*/
public constructor(cmodule: LuaWasm, shouldTraceAllocations: boolean) {
if (shouldTraceAllocations) {
Expand Down Expand Up @@ -99,9 +96,9 @@ export default class Global extends Thread {
// Higher is more important and will be evaluated first.
/**
* Registers a type extension for Lua objects.
* Higher priority is more important and will be evaluated first.
* @param {number} priority - Priority of the type extension.
* @param {LuaTypeExtension<unknown>} extension - The type extension to register.
* Higher priority is more important and will be evaluated first.
* @param priority - Priority of the type extension.
* @param extension - The type extension to register.
*/
public registerTypeExtension(priority: number, extension: LuaTypeExtension<unknown>): void {
this.typeExtensions.push({ extension, priority })
Expand All @@ -110,7 +107,7 @@ export default class Global extends Thread {

/**
* Loads a default Lua library.
* @param {LuaLibraries} library - The Lua library to load.
* @param library - The Lua library to load.
*/
public loadLibrary(library: LuaLibraries): void {
switch (library) {
Expand Down Expand Up @@ -150,8 +147,8 @@ export default class Global extends Thread {

/**
* Retrieves the value of a global variable.
* @param {string} name - The name of the global variable.
* @returns {any} - The value of the global variable.
* @param name - The name of the global variable.
* @returns - The value of the global variable.
*/
public get(name: string): any {
const type = this.lua.lua_getglobal(this.address, name)
Expand All @@ -162,8 +159,8 @@ export default class Global extends Thread {

/**
* Sets the value of a global variable.
* @param {string} name - The name of the global variable.
* @param {unknown} value - The value to set for the global variable.
* @param name - The name of the global variable.
* @param value - The value to set for the global variable.
*/
public set(name: string, value: unknown): void {
this.pushValue(value)
Expand All @@ -189,23 +186,23 @@ export default class Global extends Thread {

/**
* Gets the amount of memory used by the Lua engine. Can only be used if the state was created with the `traceAllocations` option set to true.
* @returns {number} - The amount of memory used in bytes.
* @returns - The amount of memory used in bytes.
*/
public getMemoryUsed(): number {
return this.getMemoryStatsRef().memoryUsed
}

/**
* Gets the maximum memory allowed for the Lua engine. Can only be used if the state was created with the `traceAllocations` option set to true.
* @returns {number | undefined} - The maximum memory allowed in bytes, or undefined if not set.
* @returns - The maximum memory allowed in bytes, or undefined if not set.
*/
public getMemoryMax(): number | undefined {
return this.getMemoryStatsRef().memoryMax
}

/**
* Sets the maximum memory allowed for the Lua engine. Can only be used if the state was created with the `traceAllocations` option set to true.
* @param {number | undefined} max - The maximum memory allowed in bytes, or undefined for unlimited.
* @param max - The maximum memory allowed in bytes, or undefined for unlimited.
*/
public setMemoryMax(max: number | undefined): void {
this.getMemoryStatsRef().memoryMax = max
Expand Down
12 changes: 9 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import LuaEngine from './engine'

export type LuaState = number

export type EnvironmentVariables = Record<string, string | undefined>

export type CreateEngineOptions = ConstructorParameters<typeof LuaEngine>[1]
export interface CreateEngineOptions {
openStandardLibs?: boolean
injectObjects?: boolean
enableProxy?: boolean
/** Whether to trace memory allocations. */
traceAllocations?: boolean
/** Maximum time in milliseconds a Lua function can run before being interrupted. */
functionTimeout?: number
}

export enum LuaReturn {
Ok = 0,
Expand Down

0 comments on commit 687f994

Please sign in to comment.