HOWTOs

1. How to debug

By default, a valid contract call is executed at the proxy node, but the regluar node can also manually open the contract virtual machine, verify the contract call in the blockchain, and execute the contract call.

The simple way of debugging is to output variables by using ‘pprint’, multiple parameter values will be output for the basic type. A function’s memory address will be output for a function, and a json string will be ouput for a table type value.

After that, it will increase the remote debugging function and debug the breakpoint on the remote agent node.

2. How to use event

The contract can be thrown through the emit EventName (EventArg), EventName is a custom event name, the lexical requirement of event name is the same as the variable name requirement, but 49 characters is supported at most , and EventArg is the parameter of the event, requiring the value to be a string of up to 1024 characters

The event that is thrown during the operation of the call contract is recorded into the blockchain, and the local node of the blockchain can set a callback script to monitor an event of a contract. Then, when the block chain is synchronized to a block containing the monitored event, the settings are triggered, and the callback script is also written in glua.

3. Common errors in compiling

  • The variable type is inconsistent with the assignment
  • The parameter type of the function call is inconsistent with the actual argument type
  • the type of function parameter is inconsistent with the type used in the function body, and the recommended function parameter and explicit type declaration are recommended
  • some of the code block leakage plus ` ` end ` `
  • use variables that have not been declared
  • the global variable is defined in the contract (the new global variable is not allowed to be read in the contract, but the new global variable can be defined in the script)
  • do some unallowed operator operations against nil, such as adding, subtracting, multiplication, etc
  • access the property of the non-table type value
  • others

4. Support for Chinese or other non-English language

The variable name, function name, the event name cannot be used in Chinese, start only English characters or the underscore, followed by a number of English characters or underscores or Numbers, however, the contents of the string can be used in Chinese or other languages and unicode is accepted.

5. Support for multithread

Because glua is primarily intended to run on nodes in the blockchain, it is important to consider some of the features on the block chain, especially to achieve consensus, not to support multithreaded programming.

6. How to use random number in glua

Provides two ways to get random Numbers

  • The global function get_chain_random() gets the pseudo-random number on the chain, and the random number returned by this function is the result of the calculated results, only for places that need to return the number of evenly distributed numbers
  • Global function get_waited (blocknumber) can get an int32 number, according to binary content of the block number on specified block, parameters can be used to block may also be the future of block number to get a random number, in this way can call a global function get_header_block_num before () to get a piece of block, and then use some piece of number (the number of current block number plus future block, about 10 seconds a block) as a parameter called get_waited function, if the execution of the current block chain haven’t to this block number, return 1, if the execution of the current block chain has gone beyond the block number, return according to the piece of data on an int32 number.

Take lottery as example, set get_waited parameters to the expected future block number, the lottery time allowed before then for a period of time before the lottery betting, then get_waited parameter block number hasn’t been to this piece, the return type is 1, all the people do not know to the lottery time after this function call to return the result would be how many. After the start time, call get_waited fixed returns the result of an int32 type positive number, and later call every time any node with the same parameters are fixed, the random number is determined. You can use this return value (result % 10000) / 10000 to get the random number of 4 decimal places.

7. How to implement object-oriented class inheritance and polymorphism

You can use the table type and the record type to mock object, the record class has the default attribute value, and the attribute can be a function that has the default implementation

Example:

type Person = {
    id: string default "123",
    name: string default "glua",
    age ?: int = 24,  -- -- the default value for the record attribute can be distinguished either by default or =
    fn: (number, number) => number default
            function (a: number, b: number)
                return a + b
            end
}

let p = Person()
pprint(p.id, p.name, p.age)
let n = p.fn(p.age, 2016)
pprint(n)

If you need to implement class inheritance and polymorphism like that of object-oriented language, you may create a function named ‘extend’ to call constructor function of child record class, use the function to apply child object and parent class, create new parent class object in it, and then pass child class object without properties assigned, of the parent class, to child class object. In this way, inheritance and polymorphism can be achieved. At present, no standard extend function is given, and here’s an example:

let function extend(obj, parent_class)
    let parent = parent_class(obj)
    for k, v in pairs(parent) do
        obj[k] = v
    return totable(obj)
end

type A = {
    name1: string,
    age1: int default 100
}

type B = {
    name: string,
    age: int
}

let b = B()
extend(b, A)

-- or

let c = extend(B(), A)

Another way to implement class inheritance and polymorphism is to use the setmetatable and meta-tables, we will give more documentation and examples later.