<commit_before>// Copyright 2014 dong<<EMAIL>>.
// Licensed under the MIT license.
//
// Motto - Modular Javascript environment.
package motto

import (
	"errors"
	"github.com/robertkrimen/otto"
	"io/ioutil"
	"path/filepath"
)

// ModuleLoader is declared to load a module.
type ModuleLoader func(*Motto) (otto.Value, error)

// Create module loader from javascript source code.
//
// When the loader is called, the javascript source is executed in Motto.
//
// "pwd" indicates current working directory, which might be used to search for
// modules.
func CreateLoaderFromSource(source, pwd string) ModuleLoader {
	return func(vm *Motto) (otto.Value, error) {
		// Wraps the source to create a module environment
		source = "(function(module) {var require = module.require;var exports = module.exports;\n" + source + "\n})"

		// Provide the "require" method in the module scope.
		jsRequire := func(call otto.FunctionCall) otto.Value {
			jsModuleName := call.Argument(0).String()

			moduleValue, err := vm.Require(jsModuleName, pwd)
			if err != nil {
				jsException(vm, "Error", "motto: "+err.Error())
			}

			return moduleValue
		}

		jsModule, _ := vm.Object(`({exports: {}})`)
		jsModule.Set("require", jsRequire)
		jsExports, _ := jsModule.Get("exports")

		// Run the module source, with "jsModule" as the "module" variable, "jsExports" as "this"(Nodejs capable).
		moduleReturn, err := vm.Call(source, jsExports, jsModule)
		if err != nil {
			return otto.UndefinedValue(), err
		}

		var moduleValue otto.Value
		if !moduleReturn.IsUndefined() {
			moduleValue = moduleReturn
			jsModule.Set("exports", moduleValue)
		} else {
			moduleValue, _ = jsModule.Get("exports")
		}

		return moduleValue, nil
	}
}<commit_msg>Define __dirname when loading a module<commit_after>// Copyright 2014 dong<<EMAIL>>.
// Licensed under the MIT license.
//
// Motto - Modular Javascript environment.
package motto

import (
	"errors"
	"github.com/robertkrimen/otto"
	"io/ioutil"
	"path/filepath"
)

// ModuleLoader is declared to load a module.
type ModuleLoader func(*Motto) (otto.Value, error)

// Create module loader from javascript source code.
//
// When the loader is called, the javascript source is executed in Motto.
//
// "pwd" indicates current working directory, which might be used to search for
// modules.
func CreateLoaderFromSource(source, pwd string) ModuleLoader {
	return func(vm *Motto) (otto.Value, error) {
		// Wraps the source to create a module environment
		source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;\n" + source + "\n})"

		// Provide the "require" method in the module scope.
		jsRequire := func(call otto.FunctionCall) otto.Value {
			jsModuleName := call.Argument(0).String()

			moduleValue, err := vm.Require(jsModuleName, pwd)
			if err != nil {
				jsException(vm, "Error", "motto: "+err.Error())
			}

			return moduleValue
		}

		jsModule, _ := vm.Object(`({exports: {}})`)
		jsModule.Set("require", jsRequire)
		jsModule.Set("__dirname", pwd)
		jsExports, _ := jsModule.Get("exports")

		// Run the module source, with "jsModule" as the "module" variable, "jsExports" as "this"(Nodejs capable).
		moduleReturn, err := vm.Call(source, jsExports, jsModule)
		if err != nil {
			return otto.UndefinedValue(), err
		}

		var moduleValue otto.Value
		if !moduleReturn.IsUndefined() {
			moduleValue = moduleReturn
			jsModule.Set("exports", moduleValue)
		} else {
			moduleValue, _ = jsModule.Get("exports")
		}

		return moduleValue, nil
	}
}
{'fraction_non_alphanumeric': 0.13143822122150295, 'fraction_numerical': 0.0028145229383619475, 'mean_word_length': 3.8751714677640603, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3243063', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1108, 'n_words': 337}