Loadstring: Difference between revisions

From GiderosMobile
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
<languages />
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
'''Class:''' [[(global)]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/(global)|(global)]]<br/>


=== <translate>Description</translate> ===
=== Description ===
<translate>Gets the chunk from the given string. To load and run a given string, use the idiom  
Gets the chunk from the given string.
<syntaxhighlight lang="lua">
(function) = loadstring(string,chunkname)
</syntaxhighlight>
 
Loadstring is used mainly by some scripts and by the majority of exploiting scripts.
It runs the ''string'' given inside its brackets as code. To load and run a given string, use the idiom:
 
''assert(loadstring(s))()''
 
Chunkname is used as the chunk name for error messages and debug information. When absent, chunkname defaults to the given string.


    ''assert(loadstring(s))()''
=== Parameters ===
'''string''': (string) string to load and compile<br/>
'''chunkname''': (string) is used as the chunk name for error messages and debug information '''optional'''<br/>


Chunkname is used as the chunk name for error messages and debug information. When absent, chunkname defaults to the given string.</translate>
=== Return values ===
<source lang="lua">
'''Returns''' (function) compiled chunk as a function, otherwise returns nil<br/>
(function) = loadstring(string,chunkname)
 
</source>
=== Examples ===
<syntaxhighlight lang="lua">
local test = loadstring("print('Hello world!')")
test() --> Hello world!
-- same as above
local test = loadstring("print('Hello world!')")()


=== <translate>Parameters</translate> ===
-- a calculator
'''string''': (string) <translate>string to load and compile</translate> <br/>
local mathStuff = "5+5*3"
'''chunkname''': (string) <translate>is used as the chunk name for error messages and debug information.</translate> '''optional'''<br/>
local resultOfMath = loadstring("return "..mathStuff)()
print(resultOfMath)


=== <translate>Return values</translate> ===
-- run string as code
'''<translate>Returns</translate>''' (function) <translate>compiled chunk as a function; otherwise, returns nil</translate><br/>
local Code = "print('I love loadstrings!')"
loadstring(Code)()
</syntaxhighlight>


{{(global)}}
{{(global)}}

Latest revision as of 13:34, 2 February 2025

Available since: Gideros 2011.6
Class: (global)

Description

Gets the chunk from the given string.

(function) = loadstring(string,chunkname)

Loadstring is used mainly by some scripts and by the majority of exploiting scripts. It runs the string given inside its brackets as code. To load and run a given string, use the idiom:

assert(loadstring(s))()

Chunkname is used as the chunk name for error messages and debug information. When absent, chunkname defaults to the given string.

Parameters

string: (string) string to load and compile
chunkname: (string) is used as the chunk name for error messages and debug information optional

Return values

Returns (function) compiled chunk as a function, otherwise returns nil

Examples

local test = loadstring("print('Hello world!')")
test() --> Hello world!
-- same as above
local test = loadstring("print('Hello world!')")()

-- a calculator
local mathStuff = "5+5*3"
local resultOfMath = loadstring("return "..mathStuff)()
print(resultOfMath)

-- run string as code
local Code = "print('I love loadstrings!')"
loadstring(Code)()