예제 #1
0
파일: main.go 프로젝트: e8vm/shanhu
// Build builds a set of packages.
func Build(
	input string, pkgs []string, runTests bool, logln func(s string),
) string {
	s := new(fs.Snapshot)
	if err := json.Unmarshal([]byte(input), s); err != nil {
		return jsonStr(&result{FatalErr: err.Error()})
	}

	opt := &builds.Options{
		LogLine:    logln,
		RunTests:   runTests,
		TestCycles: 1e6,
		Verbose:    true,
	}
	img, errs := s.Build(pkgs, opt)
	if errs != nil {
		return jsonStr(&result{Errs: makeResErrors(errs)})
	}

	if img == nil {
		return jsonStr(&result{NoRun: true})
	}

	outBuf := new(bytes.Buffer)
	m := arch.NewMachine(&arch.Config{
		Output: outBuf,
	})
	if err := m.LoadImage(bytes.NewReader(img)); err != nil {
		return jsonStr(newErrorRes(err))
	}

	const ncycle = 1e6
	ret, exp := m.Run(ncycle)

	res := &result{
		Output: outBuf.String(),
		Ncycle: ret,
	}
	if exp != nil {
		stackTrace := new(bytes.Buffer)
		if !arch.IsHalt(exp) {
			arch.FprintStack(stackTrace, m, exp)
		}

		res.ExcepCore = exp.Core
		res.ExcepCode = exp.Code
		res.ExcepMsg = exp.Error()
		res.StackTrace = stackTrace.String()
	}

	return jsonStr(res)
}
예제 #2
0
파일: main.go 프로젝트: e8vm/shanhu
func exec(j *shell.Job) {
	set := new(shell.Results)
	j.Results = set

	if err := shell.Run(j); err != nil {
		fmt.Println(err)
	}

	for _, obj := range set.Objs {
		switch obj := obj.Obj.(type) {
		case string:
			fmt.Println(obj)
		case *shell.Error:
			fmt.Println("error: ", obj.Err)
		case *shell.ChangeDir:
			j.Cwd = obj.NewDir
		case *shell.OpenFile:
			fmt.Println("open file: ", obj.File)
		case *shell.PrintFile:
			fmt.Println(obj.Content)
		case *shell.BuildSnapshot:
			fmt.Println("build snapshot: ", obj.Hash)
			opt := &builds.Options{
				RunTests:   obj.RunTests,
				TestCycles: 1e6,
				Verbose:    true,
			}
			image, errs := obj.Snapshot.Build(obj.Pkgs, opt)
			if errs != nil {
				for _, err := range errs {
					fmt.Println(err)
				}
				return
			}
			if image == nil {
				return
			}

			fmt.Printf("(image size: %d bytes)\n", len(image))
			n, err := arch.RunImage(image, 100000)
			fmt.Printf("(%d cycles)\n", n)
			if !arch.IsHalt(err) {
				fmt.Println(err)
			}
		}
	}
}