This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lara:docs:sheet [2017/06/20 17:43] joaobispo [Calling Aspects] |
lara:docs:sheet [2019/09/22 15:47] (current) |
||
---|---|---|---|
Line 183: | Line 183: | ||
<code lara> | <code lara> | ||
- | call optimizer: OptimizeFunctions(functions, optimizations); | + | // Current syntax |
+ | var optimizer = call OptimizeFunctions(functions, optimizations); | ||
+ | |||
+ | // Previous syntax | ||
+ | call optimizer : OptimizeFunctions(functions, optimizations); | ||
var changedFuncs = optimizer.optFuncs; | var changedFuncs = optimizer.optFuncs; | ||
Line 194: | Line 198: | ||
var optimizer = new OptimizeFunctions(functions, optimizations); | var optimizer = new OptimizeFunctions(functions, optimizations); | ||
- | call optimizer(); | + | call optimizer(); // or optimizer.call() |
var changedFuncs = optimizer.optFuncs; | var changedFuncs = optimizer.optFuncs; | ||
Line 200: | Line 204: | ||
</code> | </code> | ||
- | Aspects can also be called inside Javascript files (.js). You need to build the aspect and reference the complete path separated by dollar sign ($): | + | Aspects can also be called inside Javascript files (.js). You need to build the aspect object and reference the complete path, separated by dollar signs ($): |
<code lara> | <code lara> | ||
Line 239: | Line 243: | ||
end | end | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | If the action returns a result, we can use the following syntax: | ||
+ | |||
+ | <code lara> | ||
+ | var result = $jp.exec <action_name>; | ||
+ | </code> | ||
+ | |||
+ | If 'result' is a variable that already exists, 'var' can be omitted: | ||
+ | |||
+ | <code lara> | ||
+ | result = $jp.exec <action_name>; | ||
+ | </code> | ||
+ | |||
+ | The keyword '.exec' can be omitted if you prefix the name of the action with the target join point, and add parenthesis: | ||
+ | |||
+ | <code lara> | ||
+ | result = $jp.<action_name>(); | ||
+ | </code> | ||
+ | |||
+ | ==== Calling shell commands ==== | ||
+ | |||
+ | You can invoke shell commands from LARA using the function cmd. The first argument is the command, and the second an array with the arguments for the command: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | cmd("ls", ["-l", "-v"]); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Using LARA from the Command Line ==== | ||
+ | |||
+ | === Include Folder === | ||
+ | |||
+ | To add include folders, use the flag -i. To add more than one folder, use double quotes and separate the folders with the file separator character of your current system | ||
+ | |||
+ | <code> | ||
+ | |||
+ | larai -i "libs1;libs2" | ||
+ | |||
+ | </code> | ||
+ | |||
+ | === Passing Arguments to the Aspect === | ||
+ | |||
+ | To pass arguments to the top-level aspect use the flag -av. The input is a JSON representation of the input: | ||
+ | |||
+ | <code> | ||
+ | |||
+ | larai -av "{inputFile:'data.json',execute:true,iterations:10}" | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Codedef Sections ==== | ||
+ | |||
+ | To insert sections of code that span several lines, you can define codedef sections, which act as templates. Example: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | codedef CodeTemplate(param1, param2) %{ | ||
+ | // This code is inserted as-is, without escaping | ||
+ | |||
+ | // To apply the values of parameters, use [[]] | ||
+ | var [[param1]] = [[param2]]; | ||
+ | |||
+ | }% end | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Declared codedefs can then be used in the code as functions: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | code = CodeTemplate("varName", 2); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | ====== Miscellaneous ====== | ||
+ | |||
+ | ==== Regular Expressions ==== | ||
+ | |||
+ | Lara supports JavaScript regular expressions, for instance: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | var regex = /(return\s+)(10)(\s*;)/; | ||
+ | regex.test("return 10;"); // Returns 'true' | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Since **exec** is a LARA keyword, calling **regex.exec** is not permitted. However, you can access object properties using strings to circumvent this limitation: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | (regex['exec']("return 10;"))[2]; // Returns '10' | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Alternatively, you can also use the **match** function in strings: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | String('aaaa').match(new RegExp('a*')) // Returns 'aaaa' | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Or you can also use the **match** boolean operator ( **~=** ): | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | 'aaaa' ~= /a*/ // Returns true | ||
+ | |||
+ | </code> | ||
+ | |||
+ | ==== Importing LARA Files ==== | ||
+ | |||
+ | LARA supports importing LARA files that are present in the include path using the keyword **import**. To import a file, you have to use the path to the file from the include folder, using *.* as separator and omitting the extension of the file. For instance, if you add as include the folder *~/foo* and you want to import the file *~/foo/bar/Aspect.lara*, you can write the following code: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | import bar.Aspect; | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Import statements must be the first statements in a LARA file. LARA weavers come bundled with support for a set of imports, which are part of their API (e.g., [[http://specs.fe.up.pt/tools/clava/doc/|Clava API]]). | ||
+ | |||
+ | |||
+ | |||
+ | ==== Reading/Writing JSON Files ==== | ||
+ | |||
+ | LARA supports reading from and writing to JSON objects with the object **Io**: | ||
+ | |||
+ | <code lara> | ||
+ | |||
+ | import lara.Io; | ||
+ | |||
+ | ... | ||
+ | |||
+ | Io.writeJson("file.json", anObject); | ||
+ | var loadedObject = Io.readJson("file.json"); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ |