Haxe的JavaScript没有全球命名空间污染? [英] Haxe for javascript without global namespace pollution?

查看:184
本文介绍了Haxe的JavaScript没有全球命名空间污染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题只适用于Haxe版本< 2.10



我已经知道haxe有一段时间了,但直到昨天才真正玩过它。很好奇,我决定移植 showdown.js ,一个JavaScript端口 markdown.pl ,以haxe。这非常简单,它生成的JavaScript看起来运行良好( edit:)如果你想看到它的实际操作,请检查它 here )。

然而,我注意到生成的代码在全局命名空间中转储了大量东西。 。更糟的是,它通过为未声明的标识符分配值而不使用 var 关键字来完成,因此即使您将

例如...

  if(typeof js =='undefined')js = {} 
...
Hash = function(p){if(p === $ _)return; {
...
EReg = function(r,opt){if(r === $ _)return; {
...

我设法使用sed清理大部分内容,但是我'm也被这样的东西困扰:

  {
String.prototype .__ class__ = String;
String .__ name__ = [String];
Array.prototype .__ class__ = Array;
Array .__ name__ = [Array];
Int = {__name__:[Int]}
Dynamic = {__name__:[Dynamic]}
Float = Number;
Float .__ name__ = [Float];
Bool = {__ename__:[Bool]}
Class = {__name__:[Class]}
Enum = {}
Void = {__ename__:[Void ]}
}
{
Math .__ name__ = [Math];
Math.NaN = Number [NaN];
Math.NEGATIVE_INFINITY = Number [NEGATIVE_INFINITY];
Math.POSITIVE_INFINITY =数字[POSITIVE_INFINITY];
Math.isFinite = function(i){
return isFinite(i);
}
Math.isNaN = function(i){
return isNaN(i);




$ b $ p
$ b

这是一些非常不雅的javascript。






问题



有没有haxe的分支或克隆,吨污染全球?为了得到我想要的,或者有人已经解决了这个问题,修改haxe源代码是否值得?谷歌搜索没有很多。我愿意接受任何建议。同时,我很想看看这个东西会产生什么样的PHP代码...:D






以下是我尝试过的一些创意:

后期处理



这是我的卑微构建脚本;它在剥离东西方面做得相当不错,但它并没有抓住所有的东西。我很犹豫要删除对内置构造函数原型的修改;我相信这会破坏事情。解决所有问题可能是一项任务,如果有人已经完成了这项工作,我不想开始......

  haxe -cp〜/ Projects / wmd-new -main Markdown -js markdown.js 

echothis.Markdown =(function(){var \ $ closure,Float;> ; markdown.clean.js;

seds / ^ if(typeof js =='undefined')js = {} $ / if(typeof js =='undefined')var js = { }; / g;
s / ^ \([\x09] * \)\([\ $ _a-zA-Z0-9] * = \({\ | function \ )\)/ \ 1var \ 2 / g;
/ ^ [\x09] * \(else \)\?null; $ / d;
markdown.js >> markdown.clean.js

echoreturn Markdown}()); >> markdown.clean.js;

java -jar closure / compiler.jar --js markdown.clean.js \
--compilation_level SIMPLE_OPTIMIZATIONS \
> markdown.cc.js

- js-namespace开关节省时间

感谢Dean Burge指出命名空间切换。这几乎帮助解决了我的问题。这是我当前的构建脚本。我认为会捕获所有的全局变量...

  NS = N\ $ 

haxe -cp〜/ Projects / wmd -new -main Markdown --js-namespace $ NS -js markdown.js

#导出我们的函数并声明一些变量
echothis.markdown =(function(){var \ $ _,\ $ Main,\ $ closure,\ $ estr,js,$ NS> markdown.clean.js;

#包含null;或else null;
sed/ ^ [\x09] * \(else \)\?null; $ / d ;markdown.js>> markdown.clean.js

#关闭
echoreturn$ NS.Markdown.makeHtml}()); >> markdown.clean.js;


解决方案

我使用编译器上的命名空间切换器来清除这些全局根类型。


This question only applies to Haxe version < 2.10

I've known about haxe for a while, but never really played with it until yesterday. Being curious, I decided to port showdown.js, a javascript port of markdown.pl, to haxe. This was pretty straightforward, and the javascript it generates seems to run fine (edit: If you want to see it in action, check it out here).

However, I noticed that the generated code dumps a ton of stuff in the global namespace... and what's worse, it does it by assigning values to undeclared identifiers without using the var keyword, so they're global even if you wrap the whole thing with a closure.

For example...

if(typeof js=='undefined') js = {}
...
Hash = function(p) { if( p === $_ ) return; {
...
EReg = function(r,opt) { if( r === $_ ) return; {
...

I managed to clean most of that up with sed, but I'm also bothered by stuff like this:

{
 String.prototype.__class__ = String;
 String.__name__ = ["String"];
 Array.prototype.__class__ = Array;
 Array.__name__ = ["Array"];
 Int = { __name__ : ["Int"]}
 Dynamic = { __name__ : ["Dynamic"]}
 Float = Number;
 Float.__name__ = ["Float"];
 Bool = { __ename__ : ["Bool"]}
 Class = { __name__ : ["Class"]}
 Enum = { }
 Void = { __ename__ : ["Void"]}
}
{
 Math.__name__ = ["Math"];
 Math.NaN = Number["NaN"];
 Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
 Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
 Math.isFinite = function(i) {
  return isFinite(i);
 }
 Math.isNaN = function(i) {
  return isNaN(i);
 }
}

This is some pretty unsavory javascript.


Questions

Is there a fork or clone of haxe somewhere that doesn't pollute globals? Is it worth it to modify the haxe source to get what I want, or has someone already solved this? Googling hasn't turned up much. I'm open to any suggestions. Meanwhile, I'm dying to see what kind of PHP code this thing's going to produce... :D


Answers?

Here are some of the ideas I've tried:

postprocessing

Here's my humble build script; it does a pretty good job of stripping stuff out, but it doesn't catch everything. I'm hesitant to remove the modifications to the built-in constructor prototypes; I'm sure that would break things. Fixing everything might be a bit of a task, and I don't want to start on it if someone's already done the work...

haxe -cp ~/Projects/wmd-new -main Markdown -js markdown.js

echo "this.Markdown=(function(){ var \$closure, Float;" > markdown.clean.js;

sed "s/^if(typeof js=='undefined') js = {}$/if(typeof js=='undefined') var js = {};/g ;
     s/^\([ \x09]*\)\([\$_a-zA-Z0-9]* = \({\|function\)\)/\1var \2/g ;
      /^[ \x09]*\(else \)\?null;$/d ;
     " markdown.js >> markdown.clean.js

echo "return Markdown}());" >> markdown.clean.js;

java -jar closure/compiler.jar --js markdown.clean.js \
--compilation_level SIMPLE_OPTIMIZATIONS \
> markdown.cc.js

--js-namespace switch saves the day

Thanks to Dean Burge for pointing out the namespace switch. This pretty much solved my problem, with a minor bit of help. Here's my current build script. I think this catches all the global variables...

NS=N\$

haxe -cp ~/Projects/wmd-new -main Markdown --js-namespace $NS -js markdown.js 

# export our function and declare some vars
echo "this.markdown=(function(){var \$_,\$Main,\$closure,\$estr,js,"$NS"" > markdown.clean.js;

# strip silly lines containing "null;" or "else null;"
sed "/^[ \x09]*\(else \)\?null;$/d ;" markdown.js >> markdown.clean.js

# finish the closure
echo "return "$NS".Markdown.makeHtml}());" >> markdown.clean.js;

解决方案

I use the namespace switch on the compiler to clean those global root types up.

这篇关于Haxe的JavaScript没有全球命名空间污染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆