Browse files

[enhance] compiler, jsimp: Added pure to the jsast and utils

  • Loading branch information...
1 parent 7238af5 commit 429879b20a7b1a8b5e43471951940c4567dc5f3e @BourgerieQuentin BourgerieQuentin committed Jul 16, 2012
View
6 compiler/jslang/jsAst.ml
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -42,8 +42,10 @@ type label = string
There is a pass for transforming [Local] native to [Global] native,
so that these identifiers can be serialized, and renamed
since we clean the jsbsl.
+ The boolean associated to `global ident indicates if the corresponding value
+ is pure or not.
*)
-type native_ident = [ `global | `local ]
+type native_ident = [ `global of bool | `local ]
(**
Javascript Identifiers.
View
8 compiler/jslang/jsCons.ml
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -112,8 +112,8 @@ struct
let native ?(label=def_label()) ident =
J.Je_ident (label, J.Native (`local, ident))
- let native_global ?(label=def_label()) ident =
- J.Je_ident (label, J.Native (`global, ident))
+ let native_global ?(pure=false) ?(label=def_label()) ident =
+ J.Je_ident (label, J.Native (`global pure, ident))
let field = dot
@@ -271,5 +271,5 @@ struct
let fresh ident =
J.ExprIdent (Ident.next ("js_internal_" ^ (JsPrint.string_of_ident ident)))
let native id = J.Native (`local, id)
- let native_global id = J.Native (`global, id)
+ let native_global ?(pure=false) id = J.Native (`global pure, id)
end
View
6 compiler/jslang/jsCons.mli
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -49,7 +49,7 @@ sig
Global native ident should rather not be created directly, but produced with an analysis, except for identifer known statically to be global.
*)
val native : ?label:Annot.label -> string -> JsAst.expr
- val native_global : ?label:Annot.label -> string -> JsAst.expr
+ val native_global : ?pure:bool -> ?label:Annot.label -> string -> JsAst.expr
@@ -158,7 +158,7 @@ sig
Global native ident should rather not be created directly, but produced with an analysis.
*)
val native : string -> JsAst.ident
- val native_global : string -> JsAst.ident
+ val native_global : ?pure:bool -> string -> JsAst.ident
View
8 compiler/jslang/jsIdent.ml
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -20,7 +20,7 @@ module String = BaseString
(* -- *)
-type native_ident = [ `global | `local ]
+type native_ident = [ `global of bool | `local ]
type t =
| ExprIdent of Ident.t
@@ -31,11 +31,11 @@ let compare_native n1 n2 =
then
match n2 with
| `local -> 0
- | `global -> -1
+ | `global _ -> -1
else
match n2 with
| `local -> 1
- | `global -> 0
+ | `global _ -> 0
let compare i1 i2 =
match i1, i2 with
View
4 compiler/jslang/jsIdent.mli
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -23,7 +23,7 @@
The documentation of these type is in JsAst
*)
-type native_ident = [ `global | `local ]
+type native_ident = [ `global of bool | `local ]
type t =
| ExprIdent of Ident.t
View
4 compiler/jslang/jsParse.ml
@@ -1,5 +1,5 @@
(*
- Copyright © 2011 MLstate
+ Copyright © 2011, 2012 MLstate
This file is part of Opa.
@@ -534,7 +534,7 @@ let global_expr ?(globalize=false) expr =
JsWalk.Expr.map (
function
| J.Je_ident (loc, J.Native (`local, id)) ->
- J.Je_ident (loc, J.Native (`global, id))
+ J.Je_ident (loc, J.Native (`global false, id))
| e -> e
) expr
else
View
2 compiler/jslang/jsPrint.ml
@@ -949,7 +949,7 @@ struct
| J.Native (n, ident) -> (
match n with
- | `global ->
+ | `global _ ->
ss_push_ident acc ident
| `local ->
Format.pp_print_string f ident
View
12 compiler/jslang/jsUtils.ml
@@ -21,9 +21,9 @@ let arguments_ident = JsCons.Ident.native "arguments"
let maybe_globalize_ident local_vars = function
| J.ExprIdent _ as i -> i
- | J.Native (`global,_) as i -> i
+ | J.Native (`global _,_) as i -> i
| J.Native (`local,j) as i ->
- if JsIdentSet.mem i local_vars then i else J.Native (`global,j)
+ if JsIdentSet.mem i local_vars then i else J.Native (`global true,j)
let collect_local_var local_vars stm =
(* not going inside the functions that are inside expressions on purpose.
@@ -112,14 +112,14 @@ let export_to_global_namespace_aux stm =
JsWalk.TStatement.map
(fun stm ->
match stm with
- | J.Js_function (_, J.Native (`global, name), params, body) ->
+ | J.Js_function (_, J.Native (`global _, name), params, body) ->
JsCons.Statement.assign (prefix_global name)
(JsCons.Expr.function_ None params body)
| J.Js_function (_, ((J.ExprIdent _) as i), params, body) ->
let name = JsPrint.string_of_ident i in
JsCons.Statement.assign (prefix_global name)
(JsCons.Expr.function_ None params body)
- | J.Js_var (_, J.Native (`global, name), o) ->
+ | J.Js_var (_, J.Native (`global _, name), o) ->
let rhs =
match o with
| Some e -> e
@@ -129,12 +129,12 @@ let export_to_global_namespace_aux stm =
)
(fun e ->
match e with
- | J.Je_ident (_, J.Native (`global, "exports")) ->
+ | J.Je_ident (_, J.Native (`global _, "exports")) ->
(* Since exports is not defined in the global scope
when commonjs modules are loaded, we need to replace
it *)
JsCons.Expr.native_global "global"
- | J.Je_ident (_, J.Native (`global, name)) when
+ | J.Je_ident (_, J.Native (`global _, name)) when
name <> "global" &&
name <> "require" (* Hack to avoid require scope problem *) ->
prefix_global name
View
1 compiler/qmlcps/qmlCpsClientLib.js
@@ -408,6 +408,7 @@ function launch_schedule(){
/**
* Returns value [x] to Continuation [k].
+ * BEWARE: The compiler manipulates the return_ ident as a "pure" function.
*/
function return_(k, x){
push (new Task_from_return(k, [x]));
View
6 compiler/qmljsimp/imp_Bsl.ml
@@ -128,7 +128,7 @@ struct
match cps with
| `to_ -> (* cps return : return_(k, output) *)
JsCons.Expr.call ~pure:true
- (JsCons.Expr.ident (JsAst.Native (`global, "return_")))
+ (JsCons.Expr.ident (JsAst.Native (`global true, "return_")))
[(JsCons.Expr.ident (JsAst.Native (`local, "k")));
proj_output]
| _ -> proj_output
@@ -145,7 +145,7 @@ struct
| `to_ -> function_
| `from ->
JsCons.Expr.call ~pure:true
- (JsCons.Expr.ident (JsAst.Native (`global, "uncps")))
+ (JsCons.Expr.ident (JsAst.Native (`global true, "uncps")))
[(JsCons.Expr.ident (JsAst.Native (`local, "k")));
function_;
JsCons.Expr.string (BslKey.to_string key)
@@ -378,7 +378,7 @@ struct
else
let open JsAst in
Je_call (label,
- Je_ident (label, (Native (`global, "return_"))),
+ Je_ident (label, (Native (`global true, "return_"))),
[Je_ident (label, (Native (`local, "k"))); expr],
false)
View
2 compiler/qmljsimp/imp_Inlining.ml
@@ -481,7 +481,7 @@ let global_inlining_policy_for_function name params body =
| J.Je_undefined _ -> true
| _ -> false in
match name with
- | J.Native (_, _s) -> None
+ | J.Native (`global pure, _s) when not(pure) -> None
(* We can't inline native function because of the JavaScript
specification. As a simple example you can change dynamically the
implementation of a function... *)

0 comments on commit 429879b

Please sign in to comment.