Esempio n. 1
0
func BenchmarkFib(b *testing.B) {
	vm := gohanscript.NewVM()
	vm.LoadFile("./examples/fib.yaml")
	for n := 0; n < b.N; n++ {
		vm.Run(map[string]interface{}{})
	}
}
Esempio n. 2
0
File: cli.go Progetto: vozhyk-/gohan
func getRunCommand() cli.Command {
	return cli.Command{
		Name:      "run",
		ShortName: "run",
		Usage:     "Run Gohan script Code",
		Description: `
Run gohan script code.`,
		Flags: []cli.Flag{
			cli.StringFlag{Name: "config-file,c", Value: defaultConfigFile, Usage: "config file path"},
			cli.StringFlag{Name: "args,a", Value: "", Usage: "arguments"},
		},
		Action: func(c *cli.Context) {
			src := c.Args()[0]
			vm := gohanscript.NewVM()

			args := []interface{}{}
			flags := map[string]interface{}{}
			for _, arg := range c.Args()[1:] {
				if strings.Contains(arg, "=") {
					kv := strings.Split(arg, "=")
					flags[kv[0]] = kv[1]
				} else {
					args = append(args, arg)
				}
			}
			vm.Context.Set("args", args)
			vm.Context.Set("flags", flags)
			configFile := c.String("config-file")
			loadConfig(configFile)
			_, err := vm.RunFile(src)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
				return
			}
		},
	}
}
Esempio n. 3
0
func httpServer(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
	return func(globalContext *gohanscript.Context) (interface{}, error) {
		m := martini.Classic()
		var mutex = &sync.Mutex{}
		history := []interface{}{}
		server := map[string]interface{}{
			"history": history,
		}
		m.Handlers()
		m.Use(middleware.Logging())
		m.Use(martini.Recovery())
		rawBody := util.MaybeMap(stmt.RawData["http_server"])
		paths := util.MaybeMap(rawBody["paths"])
		middlewareCode := util.MaybeString(rawBody["middleware"])
		if middlewareCode != "" {
			vm := gohanscript.NewVM()
			err := vm.LoadString(stmt.File, middlewareCode)

			if err != nil {
				return nil, err
			}
			m.Use(func(w http.ResponseWriter, r *http.Request) {
				context := globalContext.Extend(nil)
				fillInContext(context.Data(), r, w, nil)

				reqData, _ := ioutil.ReadAll(r.Body)
				buff := ioutil.NopCloser(bytes.NewBuffer(reqData))
				r.Body = buff
				var data interface{}
				if reqData != nil {
					json.Unmarshal(reqData, &data)
				}

				context.Set("request", data)
				vm.Run(context.Data())
			})
		}
		m.Use(func(w http.ResponseWriter, r *http.Request) {
			reqData, _ := ioutil.ReadAll(r.Body)
			buff := ioutil.NopCloser(bytes.NewBuffer(reqData))
			r.Body = buff
			var data interface{}
			if reqData != nil {
				json.Unmarshal(reqData, &data)
			}
			mutex.Lock()
			server["history"] = append(server["history"].([]interface{}),
				map[string]interface{}{
					"method": r.Method,
					"path":   r.URL.String(),
					"data":   data,
				})
			mutex.Unlock()
		})
		for path, body := range paths {
			methods, ok := body.(map[string]interface{})
			if !ok {
				continue
			}
			for method, rawRouteBody := range methods {
				routeBody, ok := rawRouteBody.(map[string]interface{})
				if !ok {
					continue
				}
				code := util.MaybeString(routeBody["code"])
				vm := gohanscript.NewVM()
				err := vm.LoadString(stmt.File, code)
				if err != nil {
					return nil, err
				}
				switch method {
				case "get":
					m.Get(path, func(w http.ResponseWriter, r *http.Request, p martini.Params) {
						context := globalContext.Extend(nil)
						fillInContext(context.Data(), r, w, p)
						vm.Run(context.Data())
						serveResponse(w, context.Data())
					})
				case "post":
					m.Post(path, func(w http.ResponseWriter, r *http.Request, p martini.Params) {
						context := globalContext.Extend(nil)
						fillInContext(context.Data(), r, w, p)
						requestData, _ := middleware.ReadJSON(r)
						context.Set("request", requestData)
						vm.Run(context.Data())
						serveResponse(w, context.Data())
					})
				case "put":
					m.Put(path, func(w http.ResponseWriter, r *http.Request, p martini.Params) {
						context := globalContext.Extend(nil)
						fillInContext(context.Data(), r, w, p)
						requestData, _ := middleware.ReadJSON(r)
						context.Set("request", requestData)
						vm.Run(context.Data())
						serveResponse(w, context.Data())
					})
				case "delete":
					m.Delete(path, func(w http.ResponseWriter, r *http.Request, p martini.Params) {
						context := globalContext.Extend(nil)
						fillInContext(context.Data(), r, w, p)
						vm.Run(context.Data())
						serveResponse(w, context.Data())
					})
				}
			}
		}
		testMode := stmt.Args["test"].Value(globalContext).(bool)
		if testMode {
			ts := httptest.NewServer(m)
			server["server"] = ts
			return server, nil
		}
		m.RunOnAddr(stmt.Args["address"].Value(globalContext).(string))
		return nil, nil
	}, nil
}
Esempio n. 4
0
	}
	actual, err := minigo.Run(context)
	if err != nil {
		Fail(fmt.Sprintf("Eval failed stmt: %s failed with %s", stmt, err))
		return
	}
	if expected != actual {
		Fail(fmt.Sprintf("Stmt: %s, expected: %v, actual: %v", stmt, expected, actual))
		return
	}
}

var _ = Describe("Run minigo test", func() {
	Context("When given expresstion", func() {
		It("All test should be passed", func() {
			vm := gohanscript.NewVM()
			context := gohanscript.NewContext(vm)
			context.Set("a", 1)
			context.Set("b", 3.14)
			testExpr(context, "a+1", 2)
			testExpr(context, "a-1", 0)
			testExpr(context, "2*3", 6)
			testExpr(context, "2*3 - 1", 5)
			testExpr(context, "(3 - 1) * ( 2 - 1 ) ", 2)
			testExpr(context, "10 / 5", 2)
			testExpr(context, "b+1.0", 4.14)
			testExpr(context, "b-1.0", 2.14)
			testExpr(context, "2.3*3.0", 6.9)
			testExpr(context, "2.1 - 1.1", 1.0)
			testExpr(context, "2.1 / 3.0", 0.7)
			testExpr(context, "5 % 2", 1)