Re:A JavaScript parser and interpreter written in Go
by robzyb on Thursday November 28, 2013 @03:52AM
(#6813673)
Excuse the silly question, but why was a union type so important?
[ Parent
| on HN ]
Re:A JavaScript parser and interpreter written in Go
by barrkel on Thursday November 28, 2013 @04:55AM
(#6813859)
Interpreters typically use polymorphic locations to hold values for the interpreted language. Technically almost any type of polymorphism is enough, but the fewer indirections you can get away with, the faster you can make a simple interpreter. So that would usually mean a sum type in a functional programming language with algebraic data types, or a simple object in an OO language, or a tagged union in C and Pascal.
But for better performance, approaches like tagged pointers (often used in Lisp implementations) are useful, as it eliminates a level of indirection for most integer operations. If you can afford to use 64 bits for your values, you might consider using doubles everywhere, with invalid exponents for stuffing a shortened pointer inside the float (I believe luajit uses, or used to use, this technique; it would probably be a good match for JS as well, as JS doesn't have integers).
Re:A JavaScript parser and interpreter written in Go
by toong on Thursday November 28, 2013 @04:36AM
(#6813809)
I was wondering myself why you needed a union type.
Is it because JS has dynamic types ? var x = 5; x = "John Doe";
Re:A JavaScript parser and interpreter written in Go
by astrodust on Thursday November 28, 2013 @11:33AM
(#6815193)
It allows you to create a single, compact structure that can then be utilized in a number of ways without having to re-cast it, replace it, or otherwise reallocate it.
For example, you can have a union between a 64-bit pointer and a 32-bit type identifier plus 32-bit value. This means you can store 32-bit integers in the same space as a pointer.
Re:A JavaScript parser and interpreter written in Go
by cdcarter on Thursday November 28, 2013 @03:26PM
(#6816272)
The question is what about a JS interpreter makes this necessary.
Re:A JavaScript parser and interpreter written in Go
by ek on Thursday November 28, 2013 @05:14PM
(#6816748)
As I answered above to another commenter with a similar question, JavaScript is a dynamically typed language and representing dynamic values in a statically typed language requires a bit of thinking, and unions are a common way of doing this.
Re:A JavaScript parser and interpreter written in Go
by jessedhillon on Thursday November 28, 2013 @04:53AM
(#6813851)
The alternative is a data structure which has N-1 empty, yet allocated, slots (N is the number of possible types.) instead you can have one field indicating the type of the variable and then your code can switch which field it accedes based off that indication. Total size is one int plus size of largest type represented.
Re:A JavaScript parser and interpreter written in Go
by 1631- on Thursday November 28, 2013 @06:58AM
(#6814178)
You could potentially also have an interface that defined methods for all the basic types `interface { AsString() (string, error), etc. }` and then each basic type implements this interface and returns an error if it can't/shouldn't be represented as the requested type. A type-switch could give you the same info you get from the tag and you don't pay for type-assertions