Exemple #1
0
func (runtime *_runtime) cmpl_parse(filename string, src interface{}) (*_nodeProgram, error) {
	program, err := parser.ParseFile(nil, filename, src, runtime.parserMode())
	if err != nil {
		return nil, err
	}
	return cmpl_parse(program), nil
}
Exemple #2
0
func Test_cmpl(t *testing.T) {
	tt(t, func() {
		vm := New()

		test := func(src string, expect ...interface{}) {
			program, err := parser.ParseFile(nil, "", src, 0)
			is(err, nil)
			{
				program := cmpl_parse(program)
				value := vm.runtime.cmpl_evaluate_nodeProgram(program, false)
				if len(expect) > 0 {
					is(value, expect[0])
				}
			}
		}

		test(``, Value{})

		test(`var abc = 1; abc;`, 1)

		test(`var abc = 1 + 1; abc;`, 2)

		test(`1 + 2;`, 3)
	})
}
Exemple #3
0
func TestParse_cmpl(t *testing.T) {
	tt(t, func() {

		test := func(src string) {
			program, err := parser.ParseFile(nil, "", src, 0)
			is(err, nil)
			is(cmpl_parse(program), "!=", nil)
		}

		test(``)

		test(`var abc = 1; abc;`)

		test(`
            function abc() {
                return;
            }
        `)
	})
}
Exemple #4
0
func test(filename string) error {
	script, err := ioutil.ReadFile(filename)
	if err != nil {
		return err
	}

	if !*flag_report {
		fmt.Fprintln(os.Stdout, filename, len(script))
	}

	parse := false
	option := target[filename]

	if option != "parse" {
		vm := otto.New()
		_, err = vm.Run(string(script))
		if err != nil {
			value := err.Error()
			switch {
			case match_ReferenceError_not_defined.MatchString(value):
			case match_TypeError_undefined.MatchString(value):
			case match_lookahead.MatchString(value):
			case match_backreference.MatchString(value):
			default:
				return err
			}
			parse = true
		}
	}

	if parse {
		_, err = parser.ParseFile(nil, filename, string(script), parser.IgnoreRegExpErrors)
		if err != nil {
			return err
		}
		target[filename] = "parse"
	}

	return nil
}
Exemple #5
0
func (runtime *_runtime) parse(filename string, src interface{}) (*ast.Program, error) {
	return parser.ParseFile(nil, filename, src, runtime.parserMode())
}
Exemple #6
0
func TestOttoRun(t *testing.T) {
	tt(t, func() {
		vm := New()

		program, err := parser.ParseFile(nil, "", "", 0)
		is(err, nil)
		value, err := vm.Run(program)
		is(err, nil)
		is(value, UndefinedValue())

		program, err = parser.ParseFile(nil, "", "2 + 2", 0)
		is(err, nil)
		value, err = vm.Run(program)
		is(err, nil)
		is(value, 4)
		value, err = vm.Run(program)
		is(err, nil)
		is(value, 4)

		program, err = parser.ParseFile(nil, "", "var abc; if (!abc) abc = 0; abc += 2; abc;", 0)
		value, err = vm.Run(program)
		is(err, nil)
		is(value, 2)
		value, err = vm.Run(program)
		is(err, nil)
		is(value, 4)
		value, err = vm.Run(program)
		is(err, nil)
		is(value, 6)

		{
			src := []byte("var abc; if (!abc) abc = 0; abc += 2; abc;")
			value, err = vm.Run(src)
			is(err, nil)
			is(value, 8)

			value, err = vm.Run(bytes.NewBuffer(src))
			is(err, nil)
			is(value, 10)

			value, err = vm.Run(io.Reader(bytes.NewBuffer(src)))
			is(err, nil)
			is(value, 12)
		}

		{
			script, err := vm.Compile("", `var abc; if (!abc) abc = 0; abc += 2; abc;`)
			is(err, nil)

			value, err = vm.Run(script)
			is(err, nil)
			is(value, 14)

			value, err = vm.Run(script)
			is(err, nil)
			is(value, 16)

			is(script.String(), "// \nvar abc; if (!abc) abc = 0; abc += 2; abc;")
		}
	})
}