Skip to main content

Attach package from string

Adds code from a string as a package with the specified name

Function AttachPackageFromString(Val Lua, Val PackageName, Val Code) Export

ParameterCLI optionTypeRequiredDescription
Lua-ArbitraryLua AddIn or Lua version to run
PackageName-StringThe name of the package by which it will be available after adding
Code-StringPackage code
Returns

Map Of KeyAndValue - Add result

Caution

NOCLI: this method is not available in CLI version

1C:Enterprise/OneScript code example
Lua = OPI_Lua.CreateVM("Lua54");

OPI_Lua.AttachPackageFromString(Lua, "calc", "local M = {} function M.double(x) return x * 2 end return M");

OPI_Lua.ExecuteCodeFromString(Lua, "calc = require('calc')");

Parameters = New Array;
Parameters.Add(5);

Result = OPI_Lua.CallFunction(Lua, "calc.double", Parameters);
Result
10