How to add a new class to Google V8? - javascript

I'm a new comer in Google V8 and Javascript, and I'm trying to add a new class to Javascript using C++.
I've finished some work using Webkit's V8 binding, references are: webkit idl and v8 binding
Now I want to integrate it into V8 engine directly, by modifying V8's code instead of simply using V8's api to make a extension. In other words, I want to add a new class just like Array type in Javascript, using the same implementation mechanism.
I've searched the Internet, including docs in Google, but have only seen guides on embedding V8 with native code.
Where can I find guides about modifying V8's code?
Or where can I find docs about V8's design architecture?
Or can anyone describe how V8 implements the Array type in C++?
Thanks a lot.

Firstly, it's likely that you can actually get away with using the v8 api to do whatever it is that you want to do. You can use it to create prototypes that mostly behave the same as built-in objects, you can bind C++ function calls to JS function calls also. There's really no reason to modify v8 itself unless you need something to be extremely fast or to inspect or manipulate v8 internals. For instance, Chrome's DOM implementation uses the v8 API rather than being implemented in v8 directly. The embedder's guide actually has all the information you need to create "classes" (remember that in JS it's actually prototype inheritance): https://developers.google.com/v8/embed#templates.
That said, here's some good places to look in the source code for say, the array object. I'm not sure off any design doc, you're probably better off looking at the source.
The array object itself is here:
https://code.google.com/p/v8/source/browse/trunk/src/objects.h#8409
Some of the array api functions are implemented here (many use the same public APIs as you would for extending):
https://code.google.com/p/v8/source/browse/trunk/src/builtins.cc#511
Some of the array api functions are implemented in JavaScript: https://code.google.com/p/v8/source/browse/trunk/src/array.js
Do a search for JSArray and you'll see much more. Pay particular attention to the bits in the native code generator, because you if you really want to take advantage of some custom type written at this level, you'll want to write code to generate efficient machine code too, for a bunch of different architectures...
Edit: Looks like V8 documentation has moved (and are better) than when this answer was written, here's some quick links to useful documentation:
Wiki: https://github.com/v8/v8/wiki/Getting%20Started%20with%20Embedding
API docs: http://v8.paulfryzel.com/docs/master/index.html

Related

TypeScript General Constructs reference guide

On the TypeScript GitHub page, there's a document that contains coding guidelines, explaining the do's and don't's of TypeScript: TypeScript Coding Guidelines. It says the following:
For a variety of reasons, we avoid certain constructs, and use some of our own. Among them:
Do not use ECMAScript 5 functions; instead use those found in core.ts.
Do not use for..in statements; instead, use ts.forEach, ts.forEachKey and ts.forEachValue. Be aware of their slightly different semantics.
Try to use ts.forEach, ts.map, and ts.filter instead of loops when it is not strongly inconvenient.
This makes sense to me. TypeScript obviously has a built-in library containing some ES5 functions optimised to be used within TypeScript. What I was wondering is if there's some kind of function reference like jQuery's own documentation, where each method has an explanation, example and other info regarding compatibility and version differences.
I've searched for this on the TypeScript website and on Google, but Google insists I'm searching for 'Generic Constructs' instead of 'General Constructs' (which Microsoft calls its built-in TS library). I can't really find anything related to the general constructs that they're talking about on the site, except for the core.ts file of course, which contains the general constructs.
Does anyone have a source I could use for this?
The coding guideline you refer to is the internal guideline for the TypeScript compiler project. It is not necessary the coding guideline of how you should write your project with TypeScript.
You should think of TypeScript as just JavaScript with a gradual type system.
The closer you keep your code close to JavaScript the better.
The TypeScript handbook would be a good starting point.
You can also check out the deep dive guide and other style guides available on GitHub.

Systems Similar to NakedObjects?

A while ago I read the book on Naked Objects and was excited by the ideas. Writing only my core business logic and having the UI automatically generated? Sign me up!
Further, the potential goes beyond that. This can be a great tool in domain modeling. With the ability to directly invoke one's objects, one is encouraged to directly use one's domain objects, whereupon one can discover...
Flaws
Useful interaction patterns (for UI implementation, particularly pertinent if one holds the view that a UI is basically "scripting the domain objects")
New features.
To this end, I'm interested in any systems similar to Naked Objects. I did some leg-work, such as searching for hits under strings like "Direct Manipulation UI", but haven't found anything useful.
Do you know of any work along similar lines? I'd prefer something in PHP or JavaScript and that doesn't assume I'm running a Linux box. I know of NakedPHP and Spiro (can't find documentation for that), but they're both basically Naked Object implementations for PHP and javascript, respectively.
Do you know of any other systems?
As co-author of the Naked Objects book, I would like to add my view.
It is not uncommon that people really like the concept of the naked objects pattern, but don't like the particular UI. You didn't say how long ago you looked at the implementation, or indeed, which one. The two main open source implementations (there are a few others, but less established) are:
The Naked Objects Framework, for the .NET platform
Apache Isis, for the Java platform
both of these have more than one UI. But, let's presume that you have looked at all the default UIs and are not happy with any of them. First, most people who use either framework in an enterprise setting end up customising the default UI quite a bit, whether using just .css, or with additional JavaScript - but still keeping to the concept of a 'generic' UI that is created dynamically.
More recently, Dan Haywood and I took the idea of the naked objects pattern much further forward with the introduction of theRestful Objects specification - an ultra-pure REST API that works for any rich domain model. Both the two naked objects implementations now also implement the Restful Objects specification. The point about this is that using the RO API it is now relatively easy to design new, radically different, UIs (generic, or fully bespoke) that talk to a server implementation of RO. In other words, it is relatively easy to create your own client-side implementation of the naked objects concepts, re-using either of the two main server-side implementations.
Spiro, which you mentioned, is our first attempt to create a library of building blocks for creating such a UI, using JavaScript (TypeScript, actually) and Angular.js. And the cool thing about using the RO spec, is that you could run the same client against any server implementation. I recommend you take a good look at it.
I hit upon a few other possible key terms and found a few, although not all are in javascript.
These seem to be better fits...
Metawidget
OpenXava
JMatter
Metawidget is especially interesting. It supports javascript, and is easy to use. You can just provide it your domain model (a JSON object) and it can generate a UI for it. Further, it doesn't take over your page, so it can live in a pre-existing UI, and it allows UI customizations.
They also include a comparison page with similar products.
Another interesting one is BlueJ; it's an educational platform based on (the idea of?) Naked Objects, but can be used for smaller projects. Basically you can graphically create instances of your class via a context menu, then you can inspect and invoke the resulting object methods via the same way (potentially creating more objects in the process).
Going further afield, to looking towards any UI auto-generation, there are CRUD visualizers which are tied to the database.
Scito
SQLMaestro
Xataface
groceryCrud
I'm still researching these, especially the object ones, and am open to other suggestions.

Is there an CDI-like mechanism thinkable for JavaScript

Before diving into JavaScript I was doing a lot of Java and especially liked the concept of context dependency injection. I used Google Guice a lot.
I know that JavaScript is not statically typed. And there are modulization concepts like used by requireJs or Node.js (even did something myself).
But is it thinkable or are there already solutions to inject dependencies based on a loosely defined interface.. (hope that isn't too stupid to ask)
The closest thing I have seen to what you are asking for (I think) is Architect from tim caswell.
It allows you to register modules abilities and requirements, so you can essentially request something that can preform an action, and have Architect check all the available modules to see if any of them support such an action.
You may also want to take a look at Errai (http://www.jboss.org/errai). It brings the client and sever together using the same programming paradigm by using the GWT javascript compiler bringing CDI to the browser (they're also working on JPA in the browser as well).

Javascript C++ binding?

I have some C++ code that I want to expose to client side of a web app. Ideally, I want to write Javascript wrapper objects for my C++ classes so that I can use them clientside.
Has this been done before?. Does anyone have a link to show how this may be achieved?
There is a library to convert C++ code to javascript, it might help:
emscripten
Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey 1 . 8 . 5 and more
SpiderMonkey? is Mozilla Project's Javascript/ECMAScript engine.
Libjspp allows C++ developers to embed SpiderMonkey? simply and easily into their applications. Libjspp allows to run multiple Javascript Engines within same process which suits one engine per thread para dime which is helpful in achieving true parallisim. Also Libjspp no way stops user from running multiple threads within engine.
http://code.google.com/p/libjspp/
I guess that RPC is what you want. You'll need to wrap your functions on the server side using some sort of framework. I've not yet used it, but this one looks promising.
On the client side you use proxy objects to dispatch the function calls. The communication is handled usually either via XML-RPC or JSON-RPC. I used this client side framework and was quite content but I'm sure you'll find many others.
This is an old topi, however, I was in the exact situation right now, and all of the solutions I found on the net complicated or outdated.
Recently, I ran across a library which supports V8 engine (including the new isolation API, which makes 90% of the libraries I found outdated) and provides great exposure and interaction API.
https://github.com/QuartzTechnologies/v8bridge
I hope that my solution will help anybody.
There's a relatively new library for doing this called nbind. Maybe that would suit you? It looks very good to me, and I'm just about to start using it.
I think you want a C++ JSON parser. You should be able to find one here http://www.json.org/. It may not do all you want because it just serializes and deserializes C++ objects without any behavior, but it should be good enough. See https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser for some discussion.
If the C++ code has to be on the client, then there is no simple way to do this for a web app. A solution may involve coding plugins for the browsers you want to support, which may then be accessed from javascript code.
If, for example, you need this for a client application, that is another case. Such a thing has been done and involves linking your application to (or running from outside) with for example chromium library, or any other javascript execution engine. That way you can create bindings to C++ classes and use such objects from javascript and vice-versa. Note that this is also not a trivial solution and may be a big effort to implement (also requires additional resources).
You could for example wrap the C++ classes in PHP or Python, and then implement an API over HTTP to access the required functions.
Or if you insist on exposing the functions as JavaScript you could try using Node.js, and create an C++ add-on to wrap you classes. See the Node.js documentation here: http://nodejs.org/api/addons.html#addons_wrapping_c_objects
But either way, I don't think avoid creating some sort of API (HTTP SOAP, XML RPC) to access the functions on your server.
Though QML is not exactly Javascript, Qt is not plain C++, but what they do together seem just like what you need

What is "native JavaScript"?

Is there anything called "Native JavaScript"? I have seen this term in a job description I am going to be interviewing.
Is regular JavaScript is same as Native JavaScript?
The term "native" is very overused in JavaScript.
Colloquially, it is used as in Johan's answer: no JQuery, Moo, Dojo.
Analogous to the JNI for Java, Google's GWT and similar I-compile-down-to-JavaScript talks about the underlying implementation as being native.
The original use of native in JS, I believe, refers to objects built and defined in ECMAScript as opposed to the environment. JavaScript, as an ECMAScript language, is not intended to be self-sufficient; it is embedded in a host environment such as a Web browser, Photoshop, Acroread, etc. When you write a web client program, you will use objects such as Math, Function, Array, Window, and Button. The first three are native (independent of host environment), while the last two are non-native (supplied by the host environment). This is kind of the opposite of cdhowie's answer, which is a good answer BTW. Just interesting, though!
I'm sure there are other interpretations. My guess is that if you see this in a job description, it's probably not the last one: that definition is too academic. :)
Here is the official definition from the ECMAScript-262 Standard, Fifth Edition:
4.3.6 native object --- object in an ECMAScript implementation
whose semantics are fully defined by this specification rather
than by the host environment. NOTE Standard native objects are
defined in this specification. Some native objects are built-in;
others may be constructed during the course of execution of an
ECMAScript program.
In other words the built-ins like Math, Object, String, RegExp are native, as are any objects I make with object literals or function definitions. But host objects are the opposite. Sorry for the ramble.
Native JS, I think, it's example below:
$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")[$],_$_:++$,$_$$:
({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$$_:(!""+"")[$],$__:++$,$_$:
++$,$$__:({}+"")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};$.$_=
($.$_=$+"")[$.$_$]+($._$=$.$_[$.__$])+($.$$=($.$+"")[$.__$])+
((!$)+"")[$._$$]+($.__=$.$_[$.$$_])+($.$=(!""+"")[$.__$])+
($._=(!""+"")[$._$_])+$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+
(!""+"")[$._$$]+$.__+$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];
$.$($.$($.$$+"\""+$.$$_$+$._$+$.$$__+$._+"\\"+$.__$+$.$_$+$.$_$+
$.$$$_+"\\"+$.__$+$.$_$+$.$$_+$.__+".\\"+$.__$+$.$$_+$.$$$+
"\\"+$.__$+$.$$_+$._$_+"\\"+$.__$+$.$_$+$.__$+$.__+$.$$$_+
"(\\\"\\"+$.__$+$.__$+$.___+$.$$$_+(![]+"")[$._$_]+(![]+"")[$._$_]+
$._$+", \\"+$.__$+$.$$_+$.$$$+$._$+"\\"+$.__$+$.$$_+$._$_+
(![]+"")[$._$_]+$.$$_$+"!\\\")\\"+$.$$$+$._$$+"\"")())();
Native JavaScript is the JavaScript implemented in the browser.
Browser implementation is followed by ECMA standard and some extra method. For example, if you familiar with jQuery and want to select element by ID
$('#myId')
Native JavaScript will be
document.getElementById('myId')
Now, if you need to do some simple tasks. You might not or should not load a huge jQuery Library and execute all its functionality. Rather 10 lines of native Javascript will do the job instead of 5 lines of jQuery code
I belief native Javascript reveres to 'vanilla' javascript, so no jQuery, MooTools, etc. Just plain old javascript
Not to complicate things too much more as #Ray's answer is correct, but a new JavaScript framework for building mobile applications has emerged called NativeScript https://www.nativescript.org/
If not confusing enough, this framework allows you to write pure "vanilla" JavaScript, but call Native (iOS/Android/WindowsPhone) APIs.
It is almost like writing a "Native" app (think Objective-C/Java/C#) but using a single language (JavaScript) which is interpreted at run-time on the device.
I suspect as this library's popularity grows "Native JavaScript" may be used as a label to describe code using this framework (esp. since that's how I got to this question via Google)
Native JavaScript is all JavaScript code . Not any kind of framework codes.
It's faster and loads in the browser.
You can use for simple browser task, that does not require any database interface, or any back-end large computation work.
Using framework makes codes larger and makes browser slow.
The only context I know for the term "native JavaScript" involves writing JavaScript objects and/or functions that are implemented by the JavaScript provider and not written in JavaScript itself. For example, many of the functions you call on the window object are implemented by the browser in its native language (C++ for Firefox/Mozilla, etc.). This might be what they mean...
They might also mean vanilla/pure JavaScript (without frameworks like jQuery or Prototype). Consider contacting them and asking for clarification.
It is the same, perhaps just wondering if you've done more than just jayesh etc.
No , Native Javascript to me means 3rd party js functions that can do things that regular js cant , kind of like the Java-JNI relationship . Google JNEXT and JSNI for more info.
Again , I do not know the real context of the question , If i were you , I'd ask for clarification.

Categories

Resources