Пример #1
0
func (ctrl *JsController) addDiscoveryService(o *otto.Otto) {
	o.Set("discover", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) == 0 {
			glog.Errorf("DISCOVER: Missing arguments")
			return otto.NullValue()
		}

		url, _ := call.Argument(0).ToString()
		upstreams, err := ctrl.DiscoveryService.Get(url)
		if err != nil {
			glog.Errorf("Failed to discover upstreams: %v", err)
			return otto.NullValue()
		}

		glog.Infof("Discovered upstreams: %v", upstreams)

		result, err := o.ToValue(upstreams)
		if err != nil {
			glog.Errorf("Failed to convert: %v", err)
			return otto.NullValue()
		}

		return result
	})
}
Пример #2
0
func (p *pathObject) toValue(call otto.FunctionCall, withTags bool) otto.Value {
	it := p.buildIteratorTree()
	it.Tagger().Add(TopResultTag)
	const limit = 1
	var (
		val otto.Value
		err error
	)
	if !withTags {
		array := p.wk.runIteratorToArrayNoTags(it, limit)
		if len(array) < 1 {
			return otto.NullValue()
		}
		val, err = call.Otto.ToValue(array[0])
	} else {
		array := p.wk.runIteratorToArray(it, limit)
		if len(array) < 1 {
			return otto.NullValue()
		}
		val, err = call.Otto.ToValue(array[0])
	}
	if err != nil {
		glog.Error(err)
		return otto.NullValue()
	}
	return val
}
Пример #3
0
// pipelines in javascript are chainable, you take in a pipeline, and you return a pipeline
// we just generalize some of that logic here
func (js *JavascriptBuilder) setFunc(obj *otto.Object, token string, fn func(string, Node, otto.FunctionCall) (Node, error)) error {
	return obj.Set(token, func(call otto.FunctionCall) otto.Value {
		this, _ := call.This.Export()

		node, err := CreateNode(this)
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		node, err = fn(token, node, call)
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		o, err := node.Object()
		if err != nil {
			js.err = err
			return otto.NullValue()
		}

		js.setFunc(o, "transform", js.transform)
		js.setFunc(o, "save", js.save)

		return o.Value()
	})
}
Пример #4
0
func (wk *worker) toValueFunc(env *otto.Otto, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		it := buildIteratorTree(obj, wk.qs)
		it.Tagger().Add(TopResultTag)
		limit := 1
		var val otto.Value
		var err error
		if !withTags {
			array := wk.runIteratorToArrayNoTags(it, limit)
			if len(array) < 1 {
				return otto.NullValue()
			}
			val, err = call.Otto.ToValue(array[0])
		} else {
			array := wk.runIteratorToArray(it, limit)
			if len(array) < 1 {
				return otto.NullValue()
			}
			val, err = call.Otto.ToValue(array[0])
		}
		if err != nil {
			clog.Errorf("%v", err)
			return otto.NullValue()
		}
		return val
	}
}
Пример #5
0
func (p *pathObject) has(call otto.FunctionCall, rev bool) otto.Value {
	args := exportArgs(call.ArgumentList)
	if len(args) == 0 {
		return otto.NullValue()
	}
	via := args[0]
	if vp, ok := via.(*pathObject); ok {
		via = vp.path
	} else {
		via, ok = toQuadValue(via)
		if !ok {
			return otto.NullValue()
		}
	}
	qv, err := toQuadValues(args[1:])
	if err != nil {
		//TODO(dennwc): pass error to caller
		return otto.NullValue()
	}
	np := p.clonePath()
	if rev {
		np = np.HasReverse(via, qv...)
	} else {
		np = np.Has(via, qv...)
	}
	return outObj(call, p.new(np))
}
Пример #6
0
func (p *pathObject) save(call otto.FunctionCall, rev bool) otto.Value {
	args := exportArgs(call.ArgumentList)
	if len(args) > 2 || len(args) == 0 {
		return otto.NullValue()
	}
	vtag := args[0]
	if len(args) == 2 {
		vtag = args[1]
	}
	tag, ok := vtag.(string)
	if !ok {
		return otto.NullValue()
	}
	via := args[0]
	if vp, ok := via.(*pathObject); ok {
		via = vp.path
	} else {
		via, ok = toQuadValue(via)
		if !ok {
			return otto.NullValue()
		}
	}
	np := p.clonePath()
	if rev {
		np = np.SaveReverse(via, tag)
	} else {
		np = np.Save(via, tag)
	}
	return outObj(call, p.new(np))
}
Пример #7
0
func convertNilsToNulls(object interface{}) {
	switch object := object.(type) {
	case map[string]interface{}:
		for key, obj := range object {
			switch obj := obj.(type) {
			case map[string]interface{}:
				convertNilsToNulls(obj)
			case []interface{}:
				convertNilsToNulls(obj)
			case nil:
				object[key] = otto.NullValue()
			}
		}
	case []interface{}:
		for key, obj := range object {
			switch obj := obj.(type) {
			case map[string]interface{}:
				convertNilsToNulls(obj)
			case []interface{}:
				convertNilsToNulls(obj)
			case nil:
				object[key] = otto.NullValue()
			}
		}
	}
}
Пример #8
0
func (p *pathObject) toArray(call otto.FunctionCall, withTags bool) otto.Value {
	args := exportArgs(call.ArgumentList)
	if len(args) != 1 {
		return otto.NullValue()
	}
	limit := -1
	if len(args) > 0 {
		limit = toInt(args[0])
	}
	it := p.buildIteratorTree()
	it.Tagger().Add(TopResultTag)
	var (
		val otto.Value
		err error
	)
	if !withTags {
		array := p.wk.runIteratorToArrayNoTags(it, limit)
		val, err = call.Otto.ToValue(array)
	} else {
		array := p.wk.runIteratorToArray(it, limit)
		val, err = call.Otto.ToValue(array)
	}
	if err != nil {
		glog.Error(err)
		return otto.NullValue()
	}
	return val
}
Пример #9
0
func toValueFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		it := buildIteratorTree(obj, ses.ts)
		it.Tagger().Add(TopResultTag)
		limit := 1
		var val otto.Value
		var err error
		if !withTags {
			array := runIteratorToArrayNoTags(it, ses, limit)
			if len(array) < 1 {
				return otto.NullValue()
			}
			val, err = call.Otto.ToValue(array[0])
		} else {
			array := runIteratorToArray(it, ses, limit)
			if len(array) < 1 {
				return otto.NullValue()
			}
			val, err = call.Otto.ToValue(array[0])
		}
		if err != nil {
			glog.Error(err)
			return otto.NullValue()
		} else {
			return val
		}

	}
}
Пример #10
0
func cmpOpType(op iterator.Operator) func(call otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		args := exportArgs(call.ArgumentList)
		if len(args) != 1 {
			return otto.NullValue()
		}
		qv, ok := toQuadValue(args[0])
		if !ok {
			return otto.NullValue()
		}
		return outObj(call, cmpOperator{op: op, val: qv})
	}
}
Пример #11
0
func (j *job) runCompScript(irs [][]*gnr.InteractionResult) {
	vm := otto.New()

	injectRuntime(vm)

	if err := vm.Set("interactions", irs); err != nil {
		panic(err)
	}
	vm.Set("width", Width)
	vm.Set("height", Height)

	compositions := make([]composition, 0, 5)
	vm.Set("composition", func(call otto.FunctionCall) otto.Value {
		name, err := call.Argument(0).ToString()
		if err != nil {
			panic("First argument to composition() must be a string")
		}
		cb := call.Argument(1)
		if !cb.IsFunction() {
			panic("Second argument to composition() must be a function(x,y)")
		}
		compositions = append(compositions, composition{name, cb})
		return otto.NullValue()
	})

	if _, err := vm.Run(j.Composite); err != nil {
		panic(err)
	}

	for _, comp := range compositions {
		img := image.NewRGBA(image.Rect(0, 0, Width, Height))
		for y := 0; y < Height; y++ {
			for x := 0; x < Width; x++ {
				v, err := comp.Func.Call(otto.NullValue(), x, y)
				if err != nil {
					panic(err)
				}
				c, err := v.Export()
				if err != nil {
					panic(err)
				}
				img.Set(x, y, mapToColor(c.(map[string]interface{})))
			}
		}
		buf := &bytes.Buffer{}
		if err := png.Encode(buf, img); err != nil {
			panic(err)
		}
		j.Compositions[comp.Name] = buf.Bytes()
	}
}
Пример #12
0
func reverseGremlinChainTo(env *otto.Otto, prevObj *otto.Object, tag string) (*otto.Object, *otto.Object) {
	env.Run("var _base_object = {}")
	base, err := env.Object("_base_object")
	if err != nil {
		glog.Error(err)
		return otto.NullValue().Object(), otto.NullValue().Object()
	}
	if isVertexChain(prevObj) {
		base.Set("_gremlin_type", "vertex")
	} else {
		base.Set("_gremlin_type", "morphism")
	}
	return reverseGremlinChainHelper(env, prevObj, base, tag)
}
Пример #13
0
func (p *pathObject) ForEach(call otto.FunctionCall) otto.Value {
	it := p.buildIteratorTree()
	it.Tagger().Add(TopResultTag)
	limit := -1
	if len(call.ArgumentList) == 0 {
		return otto.NullValue()
	}
	callback := call.Argument(len(call.ArgumentList) - 1)
	args := exportArgs(call.ArgumentList[:len(call.ArgumentList)-1])
	if len(args) > 1 {
		limit = toInt(args[0])
	}
	p.wk.runIteratorWithCallback(it, callback, call, limit)
	return otto.NullValue()
}
Пример #14
0
func (p *pathObject) Filter(call otto.FunctionCall) otto.Value {
	args := exportArgs(call.ArgumentList)
	if len(args) == 0 {
		return otto.NullValue()
	}
	np := p.clonePath()
	for _, arg := range args {
		op, ok := arg.(cmpOperator)
		if !ok {
			return otto.NullValue()
		}
		np = np.Filter(op.op, op.val)
	}
	return outObj(call, p.new(np))
}
Пример #15
0
// Invokes the JS function. Not thread-safe! This is exposed for use by unit tests.
func (server *JSServer) DirectCallFunction(inputs []string) (interface{}, error) {
	if server.Before != nil {
		server.Before()
	}

	var result otto.Value
	var err error
	if server.fn.IsUndefined() {
		result = otto.UndefinedValue()
	} else {
		inputJS := make([]interface{}, len(inputs))
		for i, inputStr := range inputs {
			if inputStr == "" {
				inputJS[i] = otto.NullValue()
			} else {
				var err error
				inputJS[i], err = server.js.Object("x = " + inputStr)
				if err != nil {
					return nil, fmt.Errorf("Unparseable input %q: %s", inputStr, err)
				}
			}
		}
		result, err = server.fn.Call(server.fn, inputJS...)
	}
	if server.After != nil {
		return server.After(result, err)
	}
	return nil, err
}
Пример #16
0
func Worker(lines, out chan *string, script, preload string, wg *sync.WaitGroup) {
	defer wg.Done()
	vm := otto.New()

	if preload != "" {
		_, err := vm.Run(preload)
		if err != nil {
			log.Fatal(err)
		}
	}

	compiled, err := vm.Compile("", script)
	if err != nil {
		log.Fatal(err)
	}

	for line := range lines {
		vm.Set("input", *line)
		_, err := vm.Run(compiled)
		if err != nil {
			log.Fatal(err)
		}
		result, err := vm.Get("output")
		if result == otto.NullValue() {
			continue
		}
		if err != nil {
			log.Fatal(err)
		}
		r := result.String()
		out <- &r
	}
}
Пример #17
0
// SpawnPrefab is an otto function that takes three arguments, a string and two integer. This spawns an object.
// The string is the name of the prefab, while the int arguments are X and Y coords, respectively.
func (api *EngineInterface) SpawnPrefab(call otto.FunctionCall) otto.Value {
	prefabName, err := call.Argument(0).ToString()
	if err != nil {
		gLogger.Fatalln(err)
	}

	x, err := call.Argument(1).ToInteger()
	if err != nil {
		gLogger.Fatalln(err)
	}

	y, err := call.Argument(2).ToInteger()
	if err != nil {
		gLogger.Fatalln(err)
	}

	prefab := api.base.Prefabs().Prefab(prefabName)
	if prefab == nil {
		gLogger.Fatalln("Prefab", prefabName, "doesn't exit")
	}
	// api is a bit dirty don't you think?
	cords := prefab.Component(new(TransformComponent)).(*TransformComponent)
	cords.X = int32(x)
	cords.Y = int32(y)

	return otto.NullValue()
}
Пример #18
0
func (rc *RunContext) End(call otto.FunctionCall) otto.Value {
	log.Println("END OTTO")
	go func() {
		rc.EndChan <- true
	}()
	return otto.NullValue()
}
Пример #19
0
func (t *fetchTask) Execute(vm *otto.Otto, l *loop.Loop) error {
	var arguments []interface{}

	if t.err != nil {
		e, err := vm.Call(`new Error`, nil, t.err.Error())
		if err != nil {
			return err
		}

		arguments = append(arguments, e)
	}

	t.jsRes.Set("status", t.status)
	t.jsRes.Set("statusText", t.statusText)
	h := mustValue(t.jsRes.Get("headers")).Object()
	for k, vs := range t.headers {
		for _, v := range vs {
			if _, err := h.Call("append", k, v); err != nil {
				return err
			}
		}
	}
	t.jsRes.Set("_body", string(t.body))

	if _, err := t.cb.Call(otto.NullValue(), arguments...); err != nil {
		return err
	}

	return nil
}
Пример #20
0
func (rc *RunContext) SendMail(call otto.FunctionCall) otto.Value {

	if len(call.ArgumentList) < 3 {
		return rc.Err("sendMail called with too few parameters")
	}

	to, err := call.ArgumentList[0].ToString()
	if err != nil {
		return rc.Err(err.Error())
	}

	subject, err := call.ArgumentList[1].ToString()
	if err != nil {
		return rc.Err(err.Error())
	}

	body, err := call.ArgumentList[2].ToString()
	if err != nil {
		return rc.Err(err.Error())
	}

	rc.runner.Mailer.SendMailSimple(to, subject, body)

	return otto.NullValue()

}
Пример #21
0
func (p *pathObject) GetLimit(call otto.FunctionCall) otto.Value {
	args := exportArgs(call.ArgumentList)
	if len(args) != 1 {
		return otto.NullValue()
	}
	return p.getLimit(toInt(args[0]))
}
Пример #22
0
func toArrayFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		it := buildIteratorTree(obj, ses.ts)
		it.Tagger().Add(TopResultTag)
		limit := -1
		if len(call.ArgumentList) > 0 {
			limitParsed, _ := call.Argument(0).ToInteger()
			limit = int(limitParsed)
		}
		var val otto.Value
		var err error
		if !withTags {
			array := runIteratorToArrayNoTags(it, ses, limit)
			val, err = call.Otto.ToValue(array)
		} else {
			array := runIteratorToArray(it, ses, limit)
			val, err = call.Otto.ToValue(array)
		}

		if err != nil {
			glog.Error(err)
			return otto.NullValue()
		}
		return val
	}
}
Пример #23
0
func mapFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		it := buildIteratorTree(obj, ses.ts)
		it.Tagger().Add(TopResultTag)
		limit := -1
		if len(call.ArgumentList) == 0 {
			return otto.NullValue()
		}
		callback := call.Argument(len(call.ArgumentList) - 1)
		if len(call.ArgumentList) > 1 {
			limitParsed, _ := call.Argument(0).ToInteger()
			limit = int(limitParsed)
		}
		runIteratorWithCallback(it, ses, callback, call, limit)
		return otto.NullValue()
	}
}
Пример #24
0
func (p *pathObject) Back(call otto.FunctionCall) otto.Value {
	args := toStrings(exportArgs(call.ArgumentList))
	if len(args) != 1 {
		return otto.NullValue()
	}
	np := p.clonePath().Back(args[0])
	return outObj(call, p.new(np))
}
Пример #25
0
func (p *pathObject) Both(call otto.FunctionCall) otto.Value {
	preds, _, ok := toViaData(exportArgs(call.ArgumentList))
	if !ok {
		return otto.NullValue()
	}
	np := p.clonePath().Both(preds...)
	return outObj(call, p.new(np))
}
Пример #26
0
func (p *pathObject) LabelContext(call otto.FunctionCall) otto.Value {
	labels, tags, ok := toViaData(exportArgs(call.ArgumentList))
	if !ok {
		return otto.NullValue()
	}
	np := p.clonePath().LabelContextWithTags(tags, labels...)
	return outObj(call, p.new(np))
}
Пример #27
0
func (p *pathObject) Except(call otto.FunctionCall) otto.Value {
	ep, ok := exportAsPath(call.ArgumentList)
	if !ok {
		return otto.NullValue()
	}
	np := p.clonePath().Except(ep.path)
	return outObj(call, p.new(np))
}
Пример #28
0
func (p *pathObject) getLimit(limit int) otto.Value {
	it := p.buildIteratorTree()
	it.Tagger().Add(TopResultTag)
	p.wk.limit = limit
	p.wk.count = 0
	p.wk.runIterator(it)
	return otto.NullValue()
}
Пример #29
0
func (p *pathObject) Is(call otto.FunctionCall) otto.Value {
	args, err := toQuadValues(exportArgs(call.ArgumentList))
	if err != nil {
		//TODO(dennwc): pass error to caller
		return otto.NullValue()
	}
	np := p.clonePath().Is(args...)
	return outObj(call, p.new(np))
}
func newError(o *otto.Otto, inError error) otto.Value {
	obj := errorToJs(inError)
	jsObj, err := o.ToValue(obj)
	if err != nil {
		glog.Errorf("Error: %s", err)
		return otto.NullValue()
	}
	return jsObj
}