module

package module
v0.0.0-...-b4433d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 10 Imported by: 1

README

otto.module

An implementation of the Node.js module loading system for otto.

pkg.go.dev GitHub Actions Appveyor Codecov

Installation

$ go get -u github.com/hattya/otto.module

Usage

package main

import (
	"fmt"
	"os"

	"github.com/hattya/otto.module"
)

func main() {
	vm, err := module.New()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	file := new(module.FileLoader)
	folder := &module.FolderLoader{File: file}
	vm.Register(file)
	vm.Register(folder)
	vm.Register(&module.NodeModulesLoader{
		File:   file,
		Folder: folder,
	})

	vm.Run(`
		var path = require('path');

		console.log(path.extname('module.go'));
	`)
}

License

otto.module is distributed under the terms of the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrModule = errors.New("module not found")

Functions

func Throw

func Throw(vm *otto.Otto, err error) otto.Value

func Wrap

func Wrap(err error) error

Types

type Binding

type Binding func(*otto.Object) error

type BindingError

type BindingError struct {
	ID string
}

func (BindingError) Error

func (e BindingError) Error() string

type FileLoader

type FileLoader struct {
}

func (*FileLoader) Load

func (l *FileLoader) Load(id string) ([]byte, error)

func (*FileLoader) Resolve

func (l *FileLoader) Resolve(id, wd string) (string, error)

type FolderLoader

type FolderLoader struct {
	File Loader
}

func (*FolderLoader) Load

func (l *FolderLoader) Load(id string) ([]byte, error)

func (*FolderLoader) Resolve

func (l *FolderLoader) Resolve(id, wd string) (string, error)

type Loader

type Loader interface {
	Load(id string) ([]byte, error)
	Resolve(id, wd string) (string, error)
}

type ModuleError

type ModuleError struct {
	ID  string
	Err error
}

func (ModuleError) Error

func (e ModuleError) Error() string

type NodeModulesLoader

type NodeModulesLoader struct {
	File   Loader
	Folder Loader
}

func (*NodeModulesLoader) Load

func (l *NodeModulesLoader) Load(id string) ([]byte, error)

func (*NodeModulesLoader) Resolve

func (l *NodeModulesLoader) Resolve(id, wd string) (string, error)

type Otto

type Otto struct {
	*otto.Otto
	// contains filtered or unexported fields
}

func New

func New() (*Otto, error)

func (*Otto) Bind

func (vm *Otto) Bind(id string, fn Binding)

func (*Otto) Binding

func (vm *Otto) Binding(id string) (otto.Value, error)

func (*Otto) Load

func (vm *Otto) Load(id string) ([]byte, error)

func (*Otto) Register

func (vm *Otto) Register(l Loader)

func (*Otto) Resolve

func (vm *Otto) Resolve(id, wd string) (string, error)

type OttoError

type OttoError struct {
	Err error
}

func (OttoError) Error

func (e OttoError) Error() string

type Package

type Package struct {
	Name string `json:"name"`
	Main string `json:"main"`
}

type PackageError

type PackageError struct {
	Path string
	Err  error
}

func (PackageError) Error

func (e PackageError) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL