Moonshine

How to manage tables

Creating tables

You can create a Lua table in JavaScript at any time, using the shine.Table constructor.

var t = new shine.Table();

You can initialise the table by passing an object or array to the constructor.

var t = new shine.Table({ x: 12, y: 34 });
var t2 = new shine.Table([12, 34, 'ab']);

Manipulating tables

Direct access to the properties of a table is unadvisable. Instead use the table.getMember() and table.setMember() methods.

var dog = new shine.Table();
dog.setMember('name', 'Brian');
function createCircle(config) {
	var radius = config.getMember('radius');
}

Array-like tables can be accessed using numerical keys. Remember to access keys starting at 1.

var head = t.getMember(1);

Using standard library functions on tables

There are some standard library functions that you can use in your JavaScript. All the table functions are safe to use, which enables you to do some Lua coding in your JavaScript should the need arise.

function nilLogger (t, key) {
	console.warn ('Missing property?', key);
}

var t = new shine.Table();
var mt = new shine.Table({ __index: nilLogger });

// Insert values
shine.lib.table.insert(t, 'hello');

// Set metatable
shine.lib.setmetatable(t, mt);

console.log(t.getMember('moo'));
console.log(t.getMember(1));
Missing property?	moo
undefined
hello