From OpenSCADAWiki
Jump to: navigation, search

System-wide user objects

JavaLikeCalc provides support of the data type "Object". The data type "Object" is an associated container of properties and functions. The properties can support data of fourth basic types and other objects. Access to object properties can be done through the record of property names to the object obj.prop, through a dot, and also through the inclusion of the property name in square brackets obj["prop"]. It is obvious that the first mechanism is static, while the second lets you to specify the name of the property through a variable. The name of the property through the dot must not start with a digit and contain operations symbols; otherwise, for the first digit, the object prefix should be used — SYS.BD.SQLite.db_1s, or write in square brackets — SYS.BD.SQLite["1+s"], for operations symbols in the name. Object's properties removing you can perform by the operator "delete". Reading of an undefined property will return null-EVAL. Creating an object is carried by the keyword new: varO = new Object(). The basic definition of the object does not contain functions. Copying of an object is actually makes the reference to the original object. When you delete an object is carried out the reducing of the reference count, and when the count is set to zero then the object is removed physically.

Different components of OpenSCADA can define the basic object with special properties and functions. The standard extension of the object is an array "Array", which is created by the command varO = new Array(prm1,prm2,prm3,...,prmN). Comma-separated parameters are placed in the array in the original order. If the parameter is the only one the array is initiated by the specified number of empty elements. Peculiarity of the array is that it works with the properties as the indexes and the main mechanism of addressing is placing the index into square brackets arr[1] is accessible. Array stores the properties in its own container of the one-dimensional array. Digital properties of the array are used to access directly to the array, and the characters work as the object properties. For more details about the properties and functions of the array can be read here.

The object of regular expression "RegExp" is created by command varO = new RegExp(pat, flg), where pat — pattern of the regular expression, and flg — match flags. The object for work with regular expressions, based on the library "PCRE". In the global search set object attribute "lastIndex", which allows you to continue searching for the next function call. In the case of an unsuccessful search for the attribute "lastIndex" reset to zero. For more details about the properties and functions of the regular expression object can be read here.

For random access to the function arguments provided the arguments object, which you can refer to by the symbol "arguments". This object contains the property "length" with a number of arguments in the function and allows you to access to a value of the argument by its number or ID. Consider the enumeration of the arguments on the cycle:

args = new Array();
for(var i = 0; i < arguments.length; i++)
  args[i] = arguments[i];

The basic types have the partial properties of the object. Properties and functions of the basic types are listed below:

  • NULL type, functions:
    • bool isEVal(); — returns "true".
  • Logical type, functions:
    • bool isEVal(); bool isNaN( ); — checks the value to null-EVAL.
    • string toString(); — performs the value as the string "true" or "false".
  • real toReal(); — reads this Boolean as a real number.
  • int toInt(); — reads this Boolean as an integer number.
  • Integer and real number:
Properties:
  • MAX_VALUE — maximum value;
  • MIN_VALUE — minimum value;
  • NaN — error value.
Functions:
  • bool isEVal(); bool isNaN( ); — checks the value to null-EVAL, and NaN for Real.
  • string toExponential( int numbs = -1 ); — returns the string of the number, formatted in the exponential notation, and with the number of significant digits numbs. If numbs is missing the number of digits will have as much as needed.
  • string toFixed( int numbs = 0, int len = 0, bool sign = false ); — returns the string of the number, formatted in the notation of fixed-point, and with the number of significant digits after the decimal point numbs, for minimum length len and compulsion to the presence of a sign. If numbs is missing, the number of digits after the decimal point is equal to zero.
  • string toPrecision( int prec = -1 ); — returns the string of the number, formatted with the number of significant digits prec.
  • string toString( int base = 10, int len = -1, bool sign = false ); — returns the string of the number of the integer type, formatted with the following representation base (2-36), for minimum length len and compulsion to the presence of a sign.
  • real toReal(); — reads this integer-real as a real number.
  • int toInt(); — reads this integer-real as an integer number.
  • String:
Properties:
  • int length — string length.
Functions:
  • bool isEVal(); — checks value to null-EVAL.
  • bool isNaN( bool whole = true ); — checks the string to Not A Number and in whole for whole.
  • string charAt( int symb, string type = "" ); — extracts from the string the symbol symb for the type. These types of the symbol are supported: ""-ASCII and raw one byte code, UTF-8, UTF-16, UTF-32. In the case of UTF-8, the symbol position symb is changed to the next symbol position due to length of this symbols type is variable one.
  • int charCodeAt( int symb, string type = "" ); — extracts from the string the symbol code symb for the type. These types of the symbol are supported: ""-ASCII and raw one byte code, UTF-8, UTF-16, UTF-16LE, UTF-16BE, UTF-32, UTF-32LE, UTF-32BE. In the case of UTF-8, the symbol position symb is changed to the next symbol position due to length of this symbols type is variable one.
  • string concat( string val1, string val2, ... ); — returns a new string formed by joining the values val1 etc. to the original one.
  • int indexOf( string substr, int start = 0 ); — returns the position of the required string substr in the original row from the position start. If the initial position is not specified then the search starts from the beginning. If the search string is not found then "-1" is returned.
  • int lastIndexOf( string substr, int start = {end} ); — returns the position of the search string substr in the original one beginning from the position of start when searching from the end. If the initial position is not specified then the search begins from the end. If the search string is not found then "-1" is returned.
  • int search( string pat, string flg = "" ); — searches into the string by the pattern pat and pattern's flags flg. Returns found substring position or "-1" for else.
var rez = "Java123Script".search("script","i");  // rez = 7
  • int search( RegExp pat ); — searches into the string by the "RegExp" pattern pat. Returns found substring position or "-1" for else.
var rez = "Java123Script".search(new RegExp("script","i"));  // rez = 7
  • Array match( string pat, string flg = "" ); — calls match for the string by the pattern pat and flags flg. Returns matched substring (0) and subexpressions (>0) array. Sets "index" attribute of the array to the substring position. Sets the "input" attribute to the source string. Sets the "err" attribute to the operation error code.
var rez = "1 plus 2 plus 3".match("\\d+","g");  // rez = [1], [2], [3]
  • Array match( TRegExp pat ); — calls match for the string and "RegExp" pattern pat. Returns matched substring (0) and subexpressions (>0) array. Sets the "index" attribute of the array to substring position. Sets the "input" attribute to the source string. Sets the "err" attribute to the operation error code.
var rez = "1 plus 2 plus 3".match(new RegExp("\\d+","g"));  // rez = [1], [2], [3]
  • string slice( int beg, int end ); string substring( int beg, int end ); — returns the string extracted from the original one starting from the beg position and ending before the end (not included), numbering from zero. If the begin or end is negative, then the count is conducted from the end of the line. If the end is not specified, then the end is the end of the line. For example, the construction substring(-2) return two last symbols of the string.
  • Array split( string sep, int limit ); — returns the array of strings separated by sep with the limit of the number of elements.
  • Array split( RegExp pat, int limit ); — returns the array of strings separated by the RegExp pattern pat with the limit of the number of elements.
rez = "1,2, 3 , 4 ,5".split(new RegExp("\\s*,\\s*"));  // rez = [1], [2], [3], [4], [5]
  • string insert( int pos, string substr ); — inserts the substring substr into this string's position pos.
  • string replace( int pos, int n, string str ); — replaces substring into the position pos and length n to the string str.
rez = "Javascript".replace(4,3,"67");  // rez = "Java67ipt"
  • string replace( string substr, string str ); — replaces all the substrings substr to the string str.
rez = "123 321".replace("3","55");  // rez = "1255 5521"
  • string replace( RegExp pat, string str ); — replaces substrings by the pattern pat to the string str.
rez = "value = \"123\"".replace(new RegExp("\"([^\"]*)\"","g"),"``$1''"));  // rez = "value = ``123''"
  • real toReal(); — converts this string to a real number.
  • int toInt( int base = 0 ); — converts this string to an integer number in accordance with base (from 2 to 36). If the base is 0, then the prefix will be considered a prefix for determining the base (123-decimal; 0123-octal; 0x123-hex).
  • string {parse,parseEnd}( int pos, string sep = ".", int off = {0,{length}}, bool mergeSepSymb = false ); — gets a token with the number pos from the string when separated by sep and from the offset off (stopping on the next token begin, end for parseEnd). mergeSepSymb specifies of allowing of merging of the group of identical symbols to one separator. Result offset is returned back to off. parseEnd() does the same but from the end.
  • string parseLine( int pos, int off = 0 ); — gets a line with the number pos from the string and from the offset off. Result offset is returned back to off (stopping on the next token begin).
  • string parsePath( int pos, int offCmptbl = 0, int off = 0 ); — gets a path token with the number pos from the string and from the offset off (stopping on the next token begin) or offCmtbl (stopping on next symbol of the current token end — for compatibility). Result offset is returned back to off or offCmptbl.
  • string parsePathEnd( int pos, int off = {length} ); — gets a path token with the number pos from the string end and from the offset off (stopping on the next token end). Result offset is returned back to off.
  • string path2sep( string sep = "." ); — converts path into this string to separated by sep string.
  • string trim( string cfg = " \n\t\r" ); — trims the string at the begin and the end for the symbols cfg.