Javascript Appcelerator Titanium Mobile如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2444001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Does Appcelerator Titanium Mobile Work?
提问by Darrell Brogdon
I'm working on building an iPhone app with Titanium Mobile 1.0 and I see that it compiles down to a native iPhone binary. How does this work? Seems like it would take a lot of heavy lifting to analyze the JavaScript code and do a direct translation into Objective-C without having a superset language like 280 North's Objective-J and Cappuccino.
我正在使用 Titanium Mobile 1.0 构建 iPhone 应用程序,我发现它可以编译为原生 iPhone 二进制文件。这是如何运作的?如果没有像 280 North 的 Objective-J 和 Cappuccino 这样的超集语言,分析 JavaScript 代码并直接翻译成 Objective-C 似乎需要很多繁重的工作。
回答by jhaynie
Titanium takes your Javascript code, analyzes and preprocesses it and then pre-compiles it into a set of symbols that are resolved based on your applications uses of Titanium APIs. From this symbol hierarchy we can build a symbol dependency matrix that maps to the underlying Titanium library symbols to understand which APIs (and related dependencies, frameworks, etc) specifically your app needs. I'm using the word symbol in a semi-generic way since it's a little different based on the language. In iPhone, the symbol maps to a true C symbol that ultimately maps to a compiled .o file that has been compiled for ARM/i386 architectures. For Java, well, it's more or less a .class file, etc. Once the front end can understand your dependency matrix, we then invoke the SDK compiler (i.e. GCC for iPhone, Java for Android) to then compile your application into the final native binary.
Titanium 获取您的 Javascript 代码,对其进行分析和预处理,然后将其预编译为一组符号,这些符号根据您的应用程序对 Titanium API 的使用情况进行解析。从这个符号层次结构中,我们可以构建一个符号依赖矩阵,该矩阵映射到底层的 Titanium 库符号,以了解您的应用程序特别需要哪些 API(以及相关的依赖项、框架等)。我以半通用的方式使用符号这个词,因为它因语言而有所不同。在 iPhone 中,符号映射到一个真正的 C 符号,最终映射到一个已编译的 .o 文件,该文件已为 ARM/i386 架构编译。对于 Java,好吧,它或多或少是一个 .class 文件等。一旦前端可以理解您的依赖矩阵,我们就会调用 SDK 编译器(即 iPhone 的 GCC,
So, a simple way to think about it is that your JS code is compiled almost one to one into the representative symbols in nativeland. There's still an interpreter running in interpreted mode otherwise things like dynamic code wouldn't work. However, its much faster, much more compact and it's about as close to pure native mapping as you can get.
所以,一个简单的思考方式是,你的JS代码几乎是一对一的编译成本土的代表符号。仍然有一个解释器在解释模式下运行,否则动态代码之类的东西将无法工作。然而,它更快,更紧凑,并且尽可能接近纯原生映射。
We're obviously still got plenty of room to improve this and working on that. So far in our latest 1.0 testing, it's almost indistinguishable from the same objective-c direct code (since in most cases it's exactly mapped to that). From a CompSci standpoint, we can now however start to optimize things that a human really couldn't easily do that - much like the GCC compiler already does today.
显然,我们仍然有足够的空间来改进这一点并努力解决这个问题。到目前为止,在我们最新的 1.0 测试中,它与相同的 Objective-c 直接代码几乎没有区别(因为在大多数情况下它完全映射到那个)。然而,从 CompSci 的角度来看,我们现在可以开始优化人类确实不容易做到的事情——就像今天的 GCC 编译器已经做到的那样。
回答by Evan B.
Like jhaynie said, the application is compiled into native code, but there is still an interpreter in-place to run some javascript, which allows the application to be very dynamic.
就像 jhaynie 所说,应用程序被编译成本机代码,但仍然有一个解释器来运行一些 javascript,这使得应用程序非常动态。
回答by the-fallen
If I package my simple ample code I get a ~80MB gzip archive (original Code ~1kB). Within the package - among others - you can find my source html and js files. There are also a lot of libraries (ssl for example) shipped with the package (because you can have low-level access to a lot of things within this framework).
如果我打包我的简单代码,我会得到一个 ~80MB 的 gzip 存档(原始代码 ~1kB)。在包中 - 除其他外 - 您可以找到我的源 html 和 js 文件。包中还附带了很多库(例如 ssl)(因为您可以对这个框架内的很多东西进行低级访问)。
I think that they take your code and wrap around some kind of interpreter software and libraries. In my case it would be like if I pack my html and js code next to a tiny browser that only displays my site.
我认为他们采用了您的代码并封装了某种解释器软件和库。就我而言,这就像我将 html 和 js 代码放在一个只显示我的网站的微型浏览器旁边。
How ever, as long as the code works on every supported system in the same way its a nice thing.
然而,只要代码以相同的方式在每个受支持的系统上工作,它就是一件好事。

