1asJSVars =
2#
3# For action script, we often want to declare the variables
4# as public/protected and also to put the variables
5#
6function(..., .vars = list(...),
7          qualifier = character(), types = character())
8{
9   # Add in suitable indentation
10  vals = paste(sapply(.vars, toJSON), ";")
11
12  if(length(types)) {
13    if(is.logical(types))
14      if(types)
15         types = sapply(.vars, jsType)
16      else  # Should never happen. Caller should leave the default!
17         types  = character()
18
19    if(length(types))
20      ids = paste(names(.vars), types, sep = " : ")
21    else
22      ids = names(.vars)
23  } else
24    ids = names(.vars)
25
26  if(length(qualifier))
27    ids = paste(qualifier, ids)
28
29    # Indent the object initialization with the number of characters involved in the
30    # variable declaration.
31  len = nchar(ids)
32  vals = sapply(seq(along = ids),
33                  function(i) {
34                     gsub("\\\n", paste("\\\n", paste(rep(" ", len[i] + 3), collapse = ""), sep = ""), vals[i])
35                  })
36
37  invisible(paste(ids, vals, sep = " = ", collapse = "\n\n"))
38}
39
40setGeneric("jsType", function(x, ...) standardGeneric("jsType"))
41
42setMethod("jsType", "matrix",
43          function(x){
44            "Array"
45          })
46
47jsTypes = c("integer" = "int",
48            "numeric" = "Number",
49            "logical" = "Boolean",
50            "character" = "String"
51           )
52
53setMethod("jsType", "vector",
54          function(x){
55            if(length(x) == 1 && is.atomic(x) && length(names(x)) == 0) {
56              jsTypes[typeof(x)]
57            } else {
58               if(length(names(x)))
59                  "Object"
60               else
61                  "Array"
62            }
63          })
64
65