Пример #1
0
func main() {
	flag.Parse()

	if !*flag_underscore {
		underscore.Disable()
	}

	err := func() error {
		src, err := readSource(flag.Arg(0))
		if err != nil {
			return err
		}

		vm := otto.New()
		_, err = vm.Run(src)
		return err
	}()
	if err != nil {
		switch err := err.(type) {
		case *otto.Error:
			fmt.Print(err.String())
		default:
			fmt.Println(err)
		}
		os.Exit(64)
	}
}
Пример #2
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
}
Пример #3
0
func init() {
	piepan.Register("js", &piepan.Plugin{
		Name: "JavaScript",
		New: func(in *piepan.Instance) piepan.Environment {
			s := otto.New()
			p := &Plugin{
				instance:  in,
				state:     s,
				listeners: make(map[string][]otto.Value),
			}
			s.Set("piepan", map[string]interface{}{
				"On":         p.apiOn,
				"Disconnect": p.apiDisconnect,
				"Audio": map[string]interface{}{
					"Play":       p.apiAudioPlay,
					"IsPlaying":  p.apiAudioIsPlaying,
					"Stop":       p.apiAudioStop,
					"NewTarget":  p.apiAudioNewTarget,
					"SetTarget":  p.apiAudioSetTarget,
					"Bitrate":    p.apiAudioBitrate,
					"SetBitrate": p.apiAudioSetBitrate,
					"Volume":     p.apiAudioVolume,
					"SetVolume":  p.apiAudioSetVolume,
				},
				"File": map[string]interface{}{
					"Open": p.apiFileOpen,
				},
				"Process": map[string]interface{}{
					"New": p.apiProcessNew,
				},
				"Timer": map[string]interface{}{
					"New": p.apiTimerNew,
				},
			})
			s.Set("ENV", createEnvVars())
			return p
		},
	})
}