goja

repository·master·Indexed 27 days ago

https://github.com/dop251/goja

A JavaScript/ECMAScript runtime and parser for Go. It includes a parser to convert JavaScript source code into an Abstract Syntax Tree (AST), implementations of standard JavaScript built-ins such as Object, Boolean, BigInt, and Error, and support for ECMAScript Promises with state inspection and rejection tracking.

Tokens
19K
Snippets
20
Records
151
Agent score
92%

What's inside goja

  1. Use the ast package for JavaScript AST representation

    master

    The ast package declares types that represent a JavaScript Abstract Syntax Tree.

    Warning: The parser and AST interfaces are currently works-in-progress, specifically regarding node types, and are subject to change in future versions.

  2. Use Object methods in Goja

    master
    The goja runtime implements the standard JavaScript Object built-in, providing methods for object manipulation, property descriptor inspection, and prototype management. These methods are available on the global Object constructor and its prototype.
  3. Compile and run JavaScript with Compile and RunProgram

    master
    For better performance when running the same code multiple times, compile the code into a *goja.Program first using Compile(name, src string, strict bool). Then, execute the compiled program using RunProgram(p *Program).
  4. Initialize a new Goja Runtime

    master
    Use goja.New() to create a new instance of a JavaScript runtime. Each runtime instance is independent; you cannot pass JavaScript values between different runtimes. Multiple runtimes can be used simultaneously in different goroutines.
  5. Use BigInt in Goja

    master

    The BigInt type in Goja provides arbitrary-precision integers, following the ECMAScript specification. You can create BigInts using the BigInt() function or by converting various types (Strings, Booleans, Numbers, or Objects) into BigInt values.

    Conversion Rules:

    • String: Converted to BigInt. Throws SyntaxError if the string is invalid.
    • Boolean: true becomes 1n, false becomes 0n.
    • Number: Converted to BigInt. Throws RangeError if the number is Infinity, NaN, or a non-integer float.
    • Object: Converted via its toPrimitiveNumber() method.
    • Undefined/Null/Symbol/Number: Throws TypeError.
  6. Manage multiple files with FileSet

    master

    A FileSet represents a collection of source files. You can add files to a set and retrieve them or their positions using a compact Idx.

    • AddFile(filename, src string) int: Adds a new file and returns its Idx.
    • File(idx Idx) *File: Retrieves the File associated with the given Idx.
    • Position(idx Idx) *Position: Converts an Idx into a human-readable Position.
  7. Use the Expression interface

    master

    The Expression interface is implemented by all expression nodes in the AST. An expression represents a piece of code that evaluates to a value.

    type Expression interface {
    	Node
    	// contains filtered or unexported methods
    }
  8. Handle parsing errors with ErrorList

    master

    The parser package uses ErrorList to collect multiple parsing errors.

    • ErrorList implements the standard Go error interface via the Err() method.
    • If the list is empty, Err() returns nil.
    • You can add errors to the list using Add(position file.Position, msg string).
    • Use Sort() to order errors by position.
  9. Format source positions with Position

    master

    The Position struct describes a specific location in the source code (filename, offset, line, and column). Use the String() method to get a formatted string representation.

    Supported formats for String():

    • file:line:column (Valid position with filename)
    • line:column (Valid position without filename)
    • file (Invalid position with filename)
    • - (Invalid position without filename)
    type Position struct {
    	Filename string // The filename where the error occurred, if any
    	Offset   int    // The src offset
    	Line     int    // The line number, starting at 1
    	Column   int    // The column number, starting at 1 (The character count)
    }
    
    // Example of String() output:
    // "example.js:1:5"
  10. Understand the AST Node interface

    master

    All nodes in the Abstract Syntax Tree (AST) implement the Node interface. This interface provides methods to retrieve the character indices of the node within the source file.

    • Idx0(): Returns the index of the first character belonging to the node.
    • Idx1(): Returns the index of the first character immediately after the node.
    type Node interface {
    	Idx0() file.Idx // The index of the first character belonging to the node
    	Idx1() file.Idx // The index of the first character immediately after the node
    }