Javascript binding API - Eina Containers

Back to the JS Eina page

Currently, two data types are available: Lists and Arrays. Both represent a sequence of items and aim to have a similar interface to the native Javascript Arrays for the most common operations like push/pop and indexed get/set.

One important limitation is that when you create an Eina collection you must pass the type of object that will be stored. This is required to allow the binding do the correct conversion between Javascript types and C/C++ types due to the former's static nature.

Besides the constructor function, they differ in the way the items are stored underneath. efl.List are doubly-linked lists, with relatively fast insert/delete operations while efl.Array objects are contiguous memory regions, with fast data access (compared to Lists) but allowing to insert or delete items only at the extremities of the container.

Apart from the constructors, both containers have the exact same API.

Constructors

Syntax

new efl.List(typename);
new efl.Array(typename);

Parameters

Return type

The constructor functions receive the name of the type that will be stored on the container. The following types are supported using the respective type name:

Containers methods and operations

The methods and operations below are supported for both types of sequences.

concat(container)

Syntax

var new_container = objA.concat(objC);

Parameters

Return type

Concatenates two containers returning the result as a new container object.

It performs a shallow copy of the container. Object references are copied (i.e. changes in the object will appear in both containers) and basic types are copied.
The concatenation operation will work only on the same type of containers (e.g. lists of ints with lists of ints). Wrong arguments will raise TypeError.

getter: container[index]

Syntax

var myvar = obj[index];

Parameters

Return type

Works like Javascript Array [] operators. The items are 0-indexed, with the first element at index 0 and the last element with index equal to the number of elements of the sequence minus 1.

Usage example:

mylist[42] // Gets the 42nd element
mylist[0] // Gets the first element.
Trying to access an element with an index out of bounds will return undefined.

indexOf(target)

Syntax

var index = obj.indexOf(target);

Parameters

Return type

Searches for target in the container and returns the index if found. If not found, return -1.

Example usage:

// obj = [100, 42, 55, 42]
var idx = obj.indexOf(42); // idx = 1
var idx2 = obj.indexOf(1000); // idx2 = -1

lastIndexOf(target)

Syntax

var index = obj.lastIndexOf(target);

Parameters

Return type

Searches backwards for target, returning its index if found, or -1 otherwise.

Example code:

// obj = [100, 42, 55, 42]
var idx = obj.lastIndexOf(42) // idx = 3
var idx2 = obj.lastIndexOf(0) // idx2 = -1

length property

Syntax

obj.length

Return type

Returns the number of elements on this container.

In pure Javascript Arrays, assigning a number smaller than the size of the container to the length property will delete elements from the container. This behavior is not present on eina Arrays or Lists. Trying to assign a number to length will have no effect.

Example usage:

var last = obj[obj.length - 1]

pop()

Syntax

var value = obj.pop();

Return type

Behaves like Javascript Array.pop(), removing the last element of the list and returning it.

Example code:

// obj is [1,2,3,4,5]
var value = obj.pop(); // value now is 5 and obj is [1,2,3,4]
Trying to pop an item from an empty list will return undefined and leave the list empty.

push(value)

Syntax

var size = obj.push(item)

Parameters

Return type

Works like the Javascript Array.push(item), appending the item to the end of the sequence. For lists, a new node is created at the end. For Arrays, the item is set to the end of the array, and it may grow as needed.

The method returns the size of the container after adding the new element.

Usage example:

var mylist = new efl.List("int");
mylist.push(3);
var myarray = new efl.Array("string");
var new_size = myarray.push("Foobar");
Although Javascript is weak typed, the binding functions are rather strong typed, doing minimal type conversions, like int to floats. Trying to push an element of a different type from the one provided in the constructor will raise TypeError.
var obj = new efl.List("float");
obj.push("44"); // CRASH!!!!

setter: container[index] = value

Syntax

obj[index] = value;
var v = (obj[index] = value);

Parameters

Return type

Works like Javascript indexed setter method. It sets the value at the given index to the given value, generally not changing the container size (see note below). On success, the operation returns the value recently set.

Trying to set a value at an index equal or larger than the size of the container will extend the container up to that index and fill value up to the last one with undefined, and setting the given value in the last position.
var obj = new efl.List("int");
obj[42] = 42; // Obj now has 42 undefined's leading up to 42 in the last position.

Usage example:

var obj = new efl.List("int");
obj.push(3) // [3]
obj.push(2) // [3,2]
obj[0] = 42; // [42, 2]
As with pushing elements, trying to push an element of a different type from the one given to the constructor will raise TypeError.

slice([begin,[end]])

Syntax

var x = obj.slice();
var y = obj.slice(startIndex);
var z = obj.slice(startIndex, endIndex);

Parameters

Return type

The slice method retrieves portions of the container. The first argument is the index to start the slice operation (default is 0, beginning of the list) and the second argument indicates the index where the extraction should stop, not including it (defaults to the container size). In interval notation, it returns [begin, end).

It also performs a shallow copy like concat().

Example usage:

var same = obj.slice() // All elements from obj are in the returned container.
var other = obj.slice(1); // Everything but the first element.
var another = obj.slice (4, 10); // From the fifth element to the tenth.

toString()

Syntax

obj.toString();

Return type

Generates a comma-separated string with the representation of the items in this container.