示例#1
0
文件: time.go 项目: jmptrader/agora
func (t *TimeMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if t.ob == nil {
		// Prepare the object
		t.ob = runtime.NewObject()
		t.ob.Set(runtime.String("Date"), runtime.NewNativeFunc(t.ctx, "time.Date", t.time_Date))
		t.ob.Set(runtime.String("Now"), runtime.NewNativeFunc(t.ctx, "time.Now", t.time_Now))
		t.ob.Set(runtime.String("Sleep"), runtime.NewNativeFunc(t.ctx, "time.Sleep", t.time_Sleep))
	}
	return t.ob, nil
}
示例#2
0
文件: fmt.go 项目: jmptrader/agora
func (f *FmtMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if f.ob == nil {
		// Prepare the object
		f.ob = runtime.NewObject()
		f.ob.Set(runtime.String("Print"), runtime.NewNativeFunc(f.ctx, "fmt.Print", f.fmt_Print))
		f.ob.Set(runtime.String("Println"), runtime.NewNativeFunc(f.ctx, "fmt.Println", f.fmt_Println))
		f.ob.Set(runtime.String("Scanln"), runtime.NewNativeFunc(f.ctx, "fmt.Scanln", f.fmt_Scanln))
		f.ob.Set(runtime.String("Scanint"), runtime.NewNativeFunc(f.ctx, "fmt.Scanint", f.fmt_Scanint))
	}
	return f.ob, nil
}
示例#3
0
func (jm *JotaModule) newVec(x, y float64) *agoraVec {
	ob := runtime.NewObject()
	v := &agoraVec{
		Object: ob,
		jm:     jm,
	}
	ob.Set(runtime.String("Length"), runtime.NewNativeFunc(jm.ctx, "jota.Vec.Length", v.length))
	ob.Set(runtime.String("Sub"), runtime.NewNativeFunc(jm.ctx, "jota.Vec.Sub", v.sub))
	ob.Set(runtime.String("Angle"), runtime.NewNativeFunc(jm.ctx, "jota.Vec.Angle", v.angle))
	ob.Set(runtime.String("X"), runtime.Number(x))
	ob.Set(runtime.String("Y"), runtime.Number(y))
	return v
}
示例#4
0
文件: os.go 项目: jmptrader/agora
func (o *OsMod) newFile(f *os.File) *file {
	ob := runtime.NewObject()
	of := &file{
		ob,
		f,
		nil,
	}
	ob.Set(runtime.String("Name"), runtime.String(f.Name()))
	ob.Set(runtime.String("Close"), runtime.NewNativeFunc(o.ctx, "os.File.Close", of.closeFile))
	ob.Set(runtime.String("ReadLine"), runtime.NewNativeFunc(o.ctx, "os.File.ReadLine", of.readLine))
	ob.Set(runtime.String("Seek"), runtime.NewNativeFunc(o.ctx, "os.File.Seek", of.seek))
	ob.Set(runtime.String("Write"), runtime.NewNativeFunc(o.ctx, "os.File.Write", of.write))
	ob.Set(runtime.String("WriteLine"), runtime.NewNativeFunc(o.ctx, "os.File.WriteLine", of.writeLine))
	return of
}
示例#5
0
文件: time.go 项目: jmptrader/agora
func (t *TimeMod) newTime(tm time.Time) runtime.Val {
	ob := &_time{
		runtime.NewObject(),
		tm,
	}
	ob.Set(runtime.String("__int"), runtime.NewNativeFunc(t.ctx, "time._time.__int", func(args ...runtime.Val) runtime.Val {
		return runtime.Number(ob.t.Unix())
	}))
	ob.Set(runtime.String("__string"), runtime.NewNativeFunc(t.ctx, "time._time.__string", func(args ...runtime.Val) runtime.Val {
		return runtime.String(ob.t.Format(time.RFC3339))
	}))
	ob.Set(runtime.String("Year"), runtime.Number(tm.Year()))
	ob.Set(runtime.String("Month"), runtime.Number(tm.Month()))
	ob.Set(runtime.String("Day"), runtime.Number(tm.Day()))
	ob.Set(runtime.String("Hour"), runtime.Number(tm.Hour()))
	ob.Set(runtime.String("Minute"), runtime.Number(tm.Minute()))
	ob.Set(runtime.String("Second"), runtime.Number(tm.Second()))
	ob.Set(runtime.String("Nanosecond"), runtime.Number(tm.Nanosecond()))
	return ob
}
示例#6
0
// Not interested in any argument in this case. Note the named return values.
func (lm *LogModule) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	// Handle the panics, convert to an error
	defer runtime.PanicToError(&err)
	// Check the cache, create the return value if unavailable
	if lm.ob == nil {
		// Prepare the object
		lm.ob = runtime.NewObject()
		// Export some functions...
		lm.ob.Set(runtime.String("Printf"), runtime.NewNativeFunc(lm.ctx, "log.Printf", lm.Printf))
	}
	return lm.ob, nil
}
示例#7
0
// Not interested in any argument in this case. Note the named return values.
func (jm *JotaModule) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	// Handle the panics, convert to an error
	defer runtime.PanicToError(&err)
	// Check the cache, create the return value if unavailable
	if jm.ob == nil {
		// Prepare the object
		jm.ob = runtime.NewObject()
		// Export some functions...
		jm.ob.Set(runtime.String("Me"), runtime.NewNativeFunc(jm.ctx, "jota.Me", jm.Me))
		jm.ob.Set(runtime.String("Move"), runtime.NewNativeFunc(jm.ctx, "jota.Move", jm.Move))
		jm.ob.Set(runtime.String("MoveTowards"), runtime.NewNativeFunc(jm.ctx, "jota.MoveTowards", jm.MoveTowards))
		jm.ob.Set(runtime.String("Turn"), runtime.NewNativeFunc(jm.ctx, "jota.Turn", jm.Turn))
		jm.ob.Set(runtime.String("UseAbility"), runtime.NewNativeFunc(jm.ctx, "jota.UseAbility", jm.UseAbility))
		jm.ob.Set(runtime.String("Param"), runtime.NewNativeFunc(jm.ctx, "jota.Param", jm.Param))
		jm.ob.Set(runtime.String("NearestEnt"), runtime.NewNativeFunc(jm.ctx, "jota.NearestEnt", jm.NearestEnt))
		jm.ob.Set(runtime.String("ControlPoints"), runtime.NewNativeFunc(jm.ctx, "jota.ControlPoints", jm.ControlPoints))
		jm.ob.Set(runtime.String("NearbyEnts"), runtime.NewNativeFunc(jm.ctx, "jota.NearbyEnts", jm.NearbyEnts))
		jm.ob.Set(runtime.String("PathDir"), runtime.NewNativeFunc(jm.ctx, "jota.PathDir", jm.PathDir))
	}
	return jm.ob, nil
}
示例#8
0
func (jm *JotaModule) newEnt(gid game.Gid) *agoraEnt {
	jm.gidToAgoraEntMutex.Lock()
	defer jm.gidToAgoraEntMutex.Unlock()
	if _, ok := jm.gidToAgoraEnt[gid]; !ok {
		ob := runtime.NewObject()
		ent := &agoraEnt{
			Object: ob,
			jm:     jm,
			gid:    gid,
		}
		ob.Set(runtime.String("Pos"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.Pos", ent.pos))
		ob.Set(runtime.String("Side"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.Side", ent.side))
		ob.Set(runtime.String("Vel"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.Vel", ent.vel))
		ob.Set(runtime.String("Angle"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.Angle", ent.angle))
		ob.Set(runtime.String("IsPlayer"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.IsPlayer", ent.isType(game.EntTypePlayer)))
		ob.Set(runtime.String("IsCreep"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.IsCreep", ent.isType(game.EntTypeCreep)))
		ob.Set(runtime.String("IsControlPoint"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.IsControlPoint", ent.isType(game.EntTypeControlPoint)))
		ob.Set(runtime.String("IsObstacle"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.IsObstacle", ent.isType(game.EntTypeObstacle)))
		ob.Set(runtime.String("IsProjectile"), runtime.NewNativeFunc(jm.ctx, "jota.Ent.IsProjectile", ent.isType(game.EntTypeProjectile)))
		jm.gidToAgoraEnt[gid] = ent
	}
	return jm.gidToAgoraEnt[gid]
}
示例#9
0
func (fp *FilepathMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if fp.ob == nil {
		// Prepare the object
		fp.ob = runtime.NewObject()
		fp.ob.Set(runtime.String("Abs"), runtime.NewNativeFunc(fp.ctx, "filepath.Abs", fp.filepath_Abs))
		fp.ob.Set(runtime.String("Base"), runtime.NewNativeFunc(fp.ctx, "filepath.Base", fp.filepath_Base))
		fp.ob.Set(runtime.String("Dir"), runtime.NewNativeFunc(fp.ctx, "filepath.Dir", fp.filepath_Dir))
		fp.ob.Set(runtime.String("Ext"), runtime.NewNativeFunc(fp.ctx, "filepath.Ext", fp.filepath_Ext))
		fp.ob.Set(runtime.String("IsAbs"), runtime.NewNativeFunc(fp.ctx, "filepath.IsAbs", fp.filepath_IsAbs))
		fp.ob.Set(runtime.String("Join"), runtime.NewNativeFunc(fp.ctx, "filepath.Join", fp.filepath_Join))
	}
	return fp.ob, nil
}
示例#10
0
func (s *StringsMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if s.ob == nil {
		// Prepare the object
		s.ob = runtime.NewObject()
		s.ob.Set(runtime.String("ToLower"), runtime.NewNativeFunc(s.ctx, "strings.ToLower", s.strings_ToLower))
		s.ob.Set(runtime.String("ToUpper"), runtime.NewNativeFunc(s.ctx, "strings.ToUpper", s.strings_ToUpper))
		s.ob.Set(runtime.String("HasPrefix"), runtime.NewNativeFunc(s.ctx, "strings.HasPrefix", s.strings_HasPrefix))
		s.ob.Set(runtime.String("HasSuffix"), runtime.NewNativeFunc(s.ctx, "strings.HasSuffix", s.strings_HasSuffix))
		s.ob.Set(runtime.String("Matches"), runtime.NewNativeFunc(s.ctx, "strings.Matches", s.strings_Matches))
		s.ob.Set(runtime.String("ByteAt"), runtime.NewNativeFunc(s.ctx, "strings.ByteAt", s.strings_ByteAt))
		s.ob.Set(runtime.String("Concat"), runtime.NewNativeFunc(s.ctx, "strings.Concat", s.strings_Concat))
		s.ob.Set(runtime.String("Contains"), runtime.NewNativeFunc(s.ctx, "strings.Contains", s.strings_Contains))
		s.ob.Set(runtime.String("Index"), runtime.NewNativeFunc(s.ctx, "strings.Index", s.strings_Index))
		s.ob.Set(runtime.String("LastIndex"), runtime.NewNativeFunc(s.ctx, "strings.LastIndex", s.strings_LastIndex))
		s.ob.Set(runtime.String("Slice"), runtime.NewNativeFunc(s.ctx, "strings.Slice", s.strings_Slice))
		s.ob.Set(runtime.String("Split"), runtime.NewNativeFunc(s.ctx, "strings.Split", s.strings_Split))
		s.ob.Set(runtime.String("Join"), runtime.NewNativeFunc(s.ctx, "strings.Join", s.strings_Join))
		s.ob.Set(runtime.String("Replace"), runtime.NewNativeFunc(s.ctx, "strings.Replace", s.strings_Replace))
		s.ob.Set(runtime.String("Repeat"), runtime.NewNativeFunc(s.ctx, "strings.Repeat", s.strings_Repeat))
		s.ob.Set(runtime.String("Trim"), runtime.NewNativeFunc(s.ctx, "strings.Trim", s.strings_Trim))
	}
	return s.ob, nil
}
示例#11
0
文件: math.go 项目: jmptrader/agora
func (m *MathMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if m.ob == nil {
		// Prepare the object
		m.ob = runtime.NewObject()
		m.ob.Set(runtime.String("Pi"), runtime.Number(math.Pi))
		m.ob.Set(runtime.String("Abs"), runtime.NewNativeFunc(m.ctx, "math.Abs", m.math_Abs))
		m.ob.Set(runtime.String("Acos"), runtime.NewNativeFunc(m.ctx, "math.Acos", m.math_Acos))
		m.ob.Set(runtime.String("Acosh"), runtime.NewNativeFunc(m.ctx, "math.Acosh", m.math_Acosh))
		m.ob.Set(runtime.String("Asin"), runtime.NewNativeFunc(m.ctx, "math.Asin", m.math_Asin))
		m.ob.Set(runtime.String("Asinh"), runtime.NewNativeFunc(m.ctx, "math.Asinh", m.math_Asinh))
		m.ob.Set(runtime.String("Atan"), runtime.NewNativeFunc(m.ctx, "math.Atan", m.math_Atan))
		m.ob.Set(runtime.String("Atan2"), runtime.NewNativeFunc(m.ctx, "math.Atan2", m.math_Atan2))
		m.ob.Set(runtime.String("Atanh"), runtime.NewNativeFunc(m.ctx, "math.Atanh", m.math_Atanh))
		m.ob.Set(runtime.String("Ceil"), runtime.NewNativeFunc(m.ctx, "math.Ceil", m.math_Ceil))
		m.ob.Set(runtime.String("Cos"), runtime.NewNativeFunc(m.ctx, "math.Cos", m.math_Cos))
		m.ob.Set(runtime.String("Cosh"), runtime.NewNativeFunc(m.ctx, "math.Cosh", m.math_Cosh))
		m.ob.Set(runtime.String("Exp"), runtime.NewNativeFunc(m.ctx, "math.Exp", m.math_Exp))
		m.ob.Set(runtime.String("Floor"), runtime.NewNativeFunc(m.ctx, "math.Floor", m.math_Floor))
		m.ob.Set(runtime.String("Inf"), runtime.NewNativeFunc(m.ctx, "math.Inf", m.math_Inf))
		m.ob.Set(runtime.String("IsInf"), runtime.NewNativeFunc(m.ctx, "math.IsInf", m.math_IsInf))
		m.ob.Set(runtime.String("IsNaN"), runtime.NewNativeFunc(m.ctx, "math.IsNaN", m.math_IsNaN))
		m.ob.Set(runtime.String("Max"), runtime.NewNativeFunc(m.ctx, "math.Max", m.math_Max))
		m.ob.Set(runtime.String("Min"), runtime.NewNativeFunc(m.ctx, "math.Min", m.math_Min))
		m.ob.Set(runtime.String("NaN"), runtime.NewNativeFunc(m.ctx, "math.NaN", m.math_NaN))
		m.ob.Set(runtime.String("Pow"), runtime.NewNativeFunc(m.ctx, "math.Pow", m.math_Pow))
		m.ob.Set(runtime.String("Sin"), runtime.NewNativeFunc(m.ctx, "math.Sin", m.math_Sin))
		m.ob.Set(runtime.String("Sinh"), runtime.NewNativeFunc(m.ctx, "math.Sinh", m.math_Sinh))
		m.ob.Set(runtime.String("Sqrt"), runtime.NewNativeFunc(m.ctx, "math.Sqrt", m.math_Sqrt))
		m.ob.Set(runtime.String("Tan"), runtime.NewNativeFunc(m.ctx, "math.Tan", m.math_Tan))
		m.ob.Set(runtime.String("Tanh"), runtime.NewNativeFunc(m.ctx, "math.Tanh", m.math_Tanh))
		m.ob.Set(runtime.String("RandSeed"), runtime.NewNativeFunc(m.ctx, "math.RandSeed", m.math_RandSeed))
		m.ob.Set(runtime.String("Rand"), runtime.NewNativeFunc(m.ctx, "math.Rand", m.math_Rand))
	}
	return m.ob, nil
}
示例#12
0
文件: os.go 项目: jmptrader/agora
func (o *OsMod) Run(_ ...runtime.Val) (v runtime.Val, err error) {
	defer runtime.PanicToError(&err)
	if o.ob == nil {
		// Prepare the object
		o.ob = runtime.NewObject()
		o.ob.Set(runtime.String("TempDir"), runtime.String(os.TempDir()))
		o.ob.Set(runtime.String("PathSeparator"), runtime.String(os.PathSeparator))
		o.ob.Set(runtime.String("PathListSeparator"), runtime.String(os.PathListSeparator))
		o.ob.Set(runtime.String("DevNull"), runtime.String(os.DevNull))
		o.ob.Set(runtime.String("Exec"), runtime.NewNativeFunc(o.ctx, "os.Exec", o.os_Exec))
		o.ob.Set(runtime.String("Exit"), runtime.NewNativeFunc(o.ctx, "os.Exit", o.os_Exit))
		o.ob.Set(runtime.String("Getenv"), runtime.NewNativeFunc(o.ctx, "os.Getenv", o.os_Getenv))
		o.ob.Set(runtime.String("Getwd"), runtime.NewNativeFunc(o.ctx, "os.Getwd", o.os_Getwd))
		o.ob.Set(runtime.String("ReadFile"), runtime.NewNativeFunc(o.ctx, "os.ReadFile", o.os_ReadFile))
		o.ob.Set(runtime.String("WriteFile"), runtime.NewNativeFunc(o.ctx, "os.WriteFile", o.os_WriteFile))
		o.ob.Set(runtime.String("Open"), runtime.NewNativeFunc(o.ctx, "os.Open", o.os_Open))
		o.ob.Set(runtime.String("TryOpen"), runtime.NewNativeFunc(o.ctx, "os.TryOpen", o.os_TryOpen))
		o.ob.Set(runtime.String("Mkdir"), runtime.NewNativeFunc(o.ctx, "os.Mkdir", o.os_Mkdir))
		o.ob.Set(runtime.String("Remove"), runtime.NewNativeFunc(o.ctx, "os.Remove", o.os_Remove))
		o.ob.Set(runtime.String("RemoveAll"), runtime.NewNativeFunc(o.ctx, "os.RemoveAll", o.os_RemoveAll))
		o.ob.Set(runtime.String("Rename"), runtime.NewNativeFunc(o.ctx, "os.Rename", o.os_Rename))
		o.ob.Set(runtime.String("ReadDir"), runtime.NewNativeFunc(o.ctx, "os.ReadDir", o.os_ReadDir))
	}
	return o.ob, nil
}
示例#13
0
func TestFmtPrint(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)

	cases := []struct {
		src   []runtime.Val
		exp   string
		expln string
		start bool
	}{
		0: {
			src: []runtime.Val{runtime.Nil},
			exp: "nil",
		},
		1: {
			src:   []runtime.Val{runtime.Bool(true), runtime.Bool(false)},
			exp:   "truefalse",
			expln: "true false",
		},
		2: {
			// Ok, so print does *NOT* add spaces when the value is a native string
			src:   []runtime.Val{runtime.String("string"), runtime.Number(0), runtime.Number(-1), runtime.Number(17), runtime.String("pi"), runtime.Number(3.1415)},
			exp:   "string0-117pi3.1415",
			expln: "string 0 -1 17 pi 3.1415",
		},
		3: {
			src: []runtime.Val{runtime.String("func:"),
				runtime.NewNativeFunc(ctx, "", func(args ...runtime.Val) runtime.Val { return runtime.Nil })},
			exp:   "func:<func  (",
			expln: "func: <func  (",
			start: true,
		},
		4: {
			src: []runtime.Val{runtime.NewObject()},
			exp: "{}",
		},
	}

	fm := new(FmtMod)
	fm.SetCtx(ctx)
	buf := bytes.NewBuffer(nil)
	ctx.Stdout = buf
	for i, c := range cases {
		for j := 0; j < 2; j++ {
			var res runtime.Val
			buf.Reset()
			if j == 1 {
				if c.expln != "" {
					c.exp = c.expln
				}
				if !c.start {
					c.exp += "\n"
				}
				res = fm.fmt_Println(c.src...)
			} else {
				res = fm.fmt_Print(c.src...)
			}
			if (c.start && !strings.HasPrefix(buf.String(), c.exp)) || (!c.start && c.exp != buf.String()) {
				t.Errorf("[%d] - expected %s, got %s", i, c.exp, buf.String())
			}
			if !c.start && res.Int() != int64(len(c.exp)) {
				t.Errorf("[%d] - expected return value of %d, got %d", i, len(c.exp), res.Int())
			}
		}
	}
}