1 BrightScript 2 Reference Jan 9, 2009 Minor updates: April 22, 2010
10 This example will return a type mismatch (a$ is a string, and can not contain “invalid”). Many functions that return objects can return invalid
11 When a single precision number is converted to double precision, only the seven most significant digits will be accurate. Operators Operations i
12 When AND and OR are used for logical operations, only the necessary amount of the expression is executed. For example: print true or invalid The
13 Examples: aa=CreateObject(“roAssociativeArray”) aa[“newkey”]=”the value” print aa[“newkey”] array=CreateObject(“roArray”, 10, true) array[2]=”two”
14 Roku Objects, Interfaces, and Language Integration [note: the name of Roku Objects will change to BrightScript Components] The Roku Object archite
15 The for-each statement works on any object that has an “ifEnum” interface. These include: Array, Associative Array, List, and Message Port. Prin
16 BrightScript XML Support BrightScript supports XML via two Roku Objects, and some dedicated language features. The Roku Object roXMLElement pro
17 Will return an roXMLElement reference to the first photo (id="3131875696”). ? rsp.photos Will return an roXMLList reference containing the p
18 owner=invalid As dynamic) As Object photolist=CreateObject("roList") for each photo in xmllist photolist.Push(n
19 Garbage Collection BrightScript will automatically free strings when they are no longer used, and it will free objects when their reference count g
2 Table of Contents INTRODUCTION ... 5 STATEMENT SUMMA
20 Events Events in BrightScript center around an event loop and the “roMessagePort” Roku object. Any RokuObject can be posted to a message port. Ty
21 Threading Model A BrightScript script runs in a single thread. The general rule of thumb is that Roku Object calls are synchronous if they return
22 Scope BrightScript uses the following scoping rules: Currently all related BrightScript code must reside in one file. This set of code is known
23 Creating and Using Intrinsic Objects In most of this manual we use the term “object” to refer to a “Roku Object”. These are C or C++ components wi
24 Program Statements DIM name (dim1, dim2, …, dimK) DIM (“dimension”) is a statement that provides a short cut to creating roArray objects. It sets
25 In each case, the variable on the left side of the equals sign is assigned the value of the constant or expression on the right side. END Termina
26 execution continues with the statement following the NEXT statement. (If increment was a negative number, loop ends when counter is less than final
27 While expression / Exit While The While loop executes until expression is false. The “exit while” statement can be used to terminate a while loo
28 If BooleanExpression [ Then ] [ Block ] [ ElseIfStatement+ ] [ ElseStatement ] End If ElseIfStatement ::= ElseIf BooleanExpress
29 PRINT [#output_object], [@location], item list Prints an item or a list of items on the console, or if output_object is specified, to an object
3 Anonymous Functions ... 32 BU
30 If the console you are printing to has the interface "ifTextField" then @ specifies exactly where printing is to begin. For example:
31 Dynamic In addition to the above types, the return type can be: Void Parameters can have default values and expressions. For example: F
32 m.result=m.a+m.b end function If a function is not called from an AssociatiaveArray, then its “m” is set to an AssocicateArray that is global
33 Built-In Functions BrightScript has a small number of built-in, module scope, intrinsic functions. They are the following. Type(variable) As Strin
34 el=GetLastRunCompileError() if el=invalid then el=GetLastRunRuntimeError() if el=&hFC or el=&hE2 then return &ap
35 Global Functions BrightScript has a set of standard, module scope, functions. These functions are stored in the global object. If the compiler se
36 UpTime(dummy As Integer) As Float Returns the uptime of the system since the last reboot. RebootSystem() As Void Causes a soft reboot. ListDir(path
37 A '*' matches zero or more arbitrary characters. The character class '[...]' matches any single character specified within the
38 Global String Functions UCase(s As String) As String Converts the string to all upper case. LCase(s As String) As String Converts the string to al
39 Mid (s As String, p As Integer, [n As Integer]) As String Returns a substring of s with length n and starting at position p. n may be omitted, in
4 INT(X AS FLOAT) AS INTEGER ... 40 LOG(X AS FLOA
40 Global Math Functions The following math functions are part of global. Trig functions use or return radians, not degrees. Abs (x As Float) As Fl
41 Sgn(x As Float) As Integer Sgn(x As Integer) As Integer The "sign" function: returns -1 for X negative, 0 for X zero, and +l for X positi
42 Core Roku Objects The following set of core Roku Objects are used extensively by BrightScript, and are therefore incorporated into the language de
43 PostMessage(msg As Object) As Void roInt, roFloat, roString, roBoolean, roBrSub, roInvalid The intrinsic types Integer (“Integer”), Float (“Float”
44 automatically wrapped in an roInt by the language interpreter. When a function that expects a Roku Object as a parameter is passed an int, float,
45 CreateObject("roAssociativeArray") The ifAssociativeArray interface provides: AddReplace(key As String, value As Object) As Void o
46 The roArray class implements the ifArray and ifEnum interfaces. This object is created with two parameters: CreateObject("roArray",
47 hex or ascii base 64. Note that if you are converting a byte array to a string, and the byte array contains a zero, the string conversion will end
48 ba2.ReadFile("ByteArrayTestFile") if ba.Count()<>ba2.Count() then stop for x=0 to 4000 if ba[x]<>ba2[x] then stop next b
49 <emptytag caveman=”barney” /> Would parse such that: Name= emptytag Attributes = roAssociatveArray, with one entry {caveman, barney)
5 Introduction Roku BrightScript is a powerful scripting language that makes it easy and quick to build media and networked applications for embedded
50 root.AddAttribute("key2","value2") ne=root.AddBodyElement() ne.SetName("sub") ne.SetBody("this i
51 GetAttributes() and GetText() are similar to calling xmllist.Simplify().GetText(), xmllist.Simplify().GetAttributes(). Appendix – BrightScript De
52 Appendix – Planned Improvements Iterators for reflection Ability to use interfaces with intrinsic objects Teleportation Switch statemen
53 Appendix – BrightScript Versions BrightScript Version Matrix 9-Jan-09 HD20000 1.3 Branch HD2000 2.0 Branch Compact Main Line SnapShot Date
54 Appendix – Example Script - Snake The following code will run on any BrightSign and uses GPIO buttons 1,2,3,4 for controls. REM REM The game of S
55 Function gbInit() As Void REM REM button presses go to this message port REM m.buttons = CreateObject("roMessagePort")
56 if tick_count=6 then tick_count=0 if m.snake.MakeLonger(m.text_field) then return else
57 snake.TurnWest=snkTurnWest snake.MoveForward=snkMoveForward snake.MakeLonger=snkMakeLonger snake.AddSegment=snkAddSegment snake
58 m.seg_list.RemoveTail() else tail.AddReplace("Len",len) ' same as tail.Len=len endif return m.MakeLon
59 Reserved Words INVALID FOR POS AND PRINT LINE_NUM OR GOTO REM EACH IF RETURN NEXT NOT STEP DIM THEN STOP ELSE TO TAB END TAB OBJFUN TYPE
6 Statement Summary BrightScript supports the following familiar looking statement types: If / Then / Else If / Else / End If For / To / Next /
7 Expressions, Variables, and Types Identifiers Identifiers (names of variables, functions, labels, or object member functions or interfaces (appear a
8 Dynamic typing – Unless otherwise specified, a variable is dynamically typed. This means that the type is determined by the value assigned to it
9 Type Invalid: invalid Type String: String in quotes, eg “this is a string” Type Integer: Hex integer, eg. &HFF, or decimal integer, eg. 255 Type
Comentários a estes Manuais