简体   繁体   English

易于嵌入 javascript

[英]Ease of embedding javascript

I'm looking into scripting languages to embed into an application.我正在研究嵌入到应用程序中的脚本语言。

I've always assumed Lua was the best choice, but I've read some recent news about embedding V8 and was considering using it instead.我一直认为 Lua 是最佳选择,但我最近阅读了一些关于嵌入 V8 的新闻,并正在考虑改用它。

My question is two fold:我的问题有两个:

Does anyone with experience embedding v8 (or any javascript engine) recommend it?有嵌入 v8(或任何 javascript 引擎)经验的人推荐它吗?

How does it compare with embedding Lua?它与嵌入 Lua 相比如何?

I like that v8 has a c++ embedding API.我喜欢 v8 有一个嵌入 API 的 c++。 However the Lua API is has had lots of time to be refined (newer isn't always better and all that).然而,Lua API 有很多时间来完善(更新并不总是更好等等)。

Note: I'm not concerned with which language/library is better or which has better performance.注意:我不关心哪种语言/库更好或性能更好。 I'm only asking about ease of embedding.我只是在询问是否易于嵌入。

v8 is just ok . v8还可以。 I tried to use it as a script interpreter for a video game some time ago with mixed results.前段时间我尝试将它用作视频游戏的脚本解释器,但结果好坏参半。 On the one hand, it is very fast and the API is simple;一方面,它非常快,API 很简单; but on the other hand it doesn't really do a good job of encapsulating the interpreter's state.但另一方面,它并不能很好地封装解释器的 state。 Because the code base is littered with global variables, you are basically shit out of luck if you need to reset v8 in the middle of an application, or run it in parallel from multiple threads.因为代码库中充斥着全局变量,如果您需要在应用程序中间重置 v8,或者从多个线程并行运行它,那么您基本上是不走运的。 These design decisions are understandable from the perspective of Chrome's one-process-per-VM model, but make it somewhat awkward to integrate into something like a game where you might want to run multiple VMs at once (for example in a game server back end), or have some way to quickly serialize/reset the state of the whole interpreter.从 Chrome 的每个 VM 一个进程 model 的角度来看,这些设计决策是可以理解的,但是集成到您可能希望同时运行多个 VM 的游戏中(例如在游戏服务器后端),或者有一些方法可以快速序列化/重置整个解释器的 state。

For these reasons, I would actually recommend you try giving Lua a second chance.由于这些原因,我实际上建议您尝试再给 Lua 一次机会。 As a language it tends to be much better suited for game programming tasks, plus it has a few nifty features which make game scripting way more convenient (for example, coroutines).作为一种语言,它往往更适合游戏编程任务,而且它有一些漂亮的特性,使游戏脚本更方便(例如,协程)。

There was a recent post on HackerNews about the author of Nginx discussing the (non-) suitability of V8 as an embedded scripting language: http://news.ycombinator.net/item?id=2519674 HackerNews 最近有一篇关于 Nginx 的作者讨论 V8 作为嵌入式脚本语言的(非)适用性的帖子: http://news.ycombinator.net/item?id=2519674

Lua is definitely more geared towards general embedding purposes, while V8 probably can be made to work somehow, if you prefer the familiarity of Javascript. Lua 肯定更适合一般嵌入目的,而 V8 可能可以以某种方式工作,如果您更喜欢 Javascript 的熟悉度。

Lua is trivial to embed, but the extension API is lower level than V8's. Lua 嵌入起来很简单,但扩展 API 的级别低于 V8。 It's a stack based API and you have a handful of primitives to work with.它是一个基于堆栈的 API,您可以使用一些原语。 It's no less powerful, it's very robust and doesn't limit you in any way, and if you just want to export global functions into the language, it's a no-brainer.它同样强大,非常健壮,不会以任何方式限制你,如果你只是想将全局函数导出到语言中,那是不费吹灰之力的。 However, exporting C++ objects into Lua requires that you understand Lua's metatables and you may find it very confusing at first.但是,将 C++ 对象导出到 Lua 需要您了解 Lua 的元表,一开始您可能会觉得很困惑。 V8 probably makes that more straightforward. V8 可能使这更简单。

If you want a Lua embedding API that does more work for you, there are libraries like Luabind or ToLua++.如果您想要嵌入 API 的 Lua 对您有更多帮助,可以使用 Luabind 或 ToLua++ 等库。 Lua doesn't make you pay for what you don't use. Lua 不会让您为不使用的东西付费。

I personaly would not Javascript over Lua.个人不会 Javascript 超过 Lua。 Javascript is a remarkably good language, given that one engineer wrote it in a few weeks, but Lua had a lot more time and thought put into it. Javascript 是一门非常出色的语言,因为一位工程师在几周内编写了它,但 Lua 有更多的时间和思想投入其中。 It's a CS gem, making the most of a small set of carefully chosen concepts.它是一个 CS 宝石,充分利用了一小部分精心挑选的概念。 It does everything Javascript does, but better.它可以完成 Javascript 所做的一切,但更好。 It has proper lexical scoping, tail recursion, a very powerful metaprogramming facility which can emulate Javascript's prototype-based inheritence (among other things), coroutines, etc. It's just a cleaner, better language.它具有适当的词法作用域、尾递归、非常强大的元编程工具,可以模拟 Javascript 的基于原型的继承(除其他外)、协程等。它只是一种更清洁、更好的语言。

One reason I might choose Javascript over it is if I knew my audience already knew Javascript, but I did that once with TCL and lived to regret it (though JS is nowhere near as bad as TCL; you can't go that wrong here).我可能会选择 Javascript 的一个原因是,如果我知道我的观众已经知道 Javascript 但我曾经在 TCL 上做过一次并且活到后悔(尽管 JS 远没有 TCL 糟糕;你不能在这里错了) .

My personal experience of embedding Lua was that it sucked quite horrifically.我个人嵌入 Lua 的经验是它非常糟糕。 The Lua API is only designed for C, and it shows. Lua API 仅为 C 设计,它显示了。 You could get various wrapping libraries, but they have their own problems.您可以获得各种包装库,但它们有自己的问题。

I haven't tried V8, but the brief overview seems to think that it has useful things like RAII and templates, so I'd vote for that.我没有尝试过 V8,但简要概述似乎认为它具有 RAII 和模板等有用的东西,所以我会投赞成票。

Unfortunately I have no experience embedding V8, so I can't directly answer your question, but I've found embedding Lua to be super easy.不幸的是我没有嵌入 V8 的经验,所以我不能直接回答你的问题,但我发现嵌入 Lua 非常容易。 The C api is verbose, but also very simple and easy to get to grips with, and manages the transitions between Lua and C very effectively. The C api is verbose, but also very simple and easy to get to grips with, and manages the transitions between Lua and C very effectively.

If C++ is your preferred language, I believe Lua also compiles cleanly as C++ and there are C++ wrappers available for it as well. If C++ is your preferred language, I believe Lua also compiles cleanly as C++ and there are C++ wrappers available for it as well.

v8 can manipulate JSON. v8 可以操作 JSON。 and purely lua can't.而纯粹的 lua 不能。 However, lua has many libraries that is enough to customize your application.但是,lua 有许多库足以自定义您的应用程序。 But v8 have no libraries.但是 v8 没有库。 v8 provide only javascript engine. v8 仅提供 javascript 引擎。 For example, we can't write a file with only v8.例如,我们不能只用 v8 编写文件。 For writing a file with v8, you should add API that can access from javascript.要使用 v8 编写文件,您应该添加可以从 javascript 访问的 API。 If you hope powerful embeding easily, like network access, automation, etc, you hava better to use lua.如果您希望轻松实现强大的嵌入,例如网络访问、自动化等,您最好使用 lua。 Or if you hope beautiful, use v8.或者,如果您希望美观,请使用 v8。 :) :)

Sorry for my poor english.对不起我糟糕的英语。

I would suggest v8.我建议v8。 Google tends to make nice API's, and it looks pretty easy to embed. Google 倾向于制作漂亮的 API,而且看起来很容易嵌入。

I won't say anything about which is a "better" language, since that is obviously subjective, and you said you don't want to hear that advice.我不会说什么是“更好”的语言,因为这显然是主观的,你说你不想听那个建议。 But I can say that Javascript sure has a lot of people who know how to use it.但是我可以说Javascript肯定有很多人知道如何使用它。

To reflect your point: just because a scripting language is old, does not make it more refined.反映您的观点:仅仅因为脚本语言陈旧,并不会使其更加精致。 Otherwise, bring on Cobol/Fortran/Assembly over C++.否则,通过 C++ 引入 Cobol/Fortran/Assembly。

I'd choose v8 over Lua.我会选择 v8 而不是 Lua。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM