Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is no adaptation for a=${test} #521

Open
youkpan opened this issue May 5, 2024 · 7 comments
Open

There is no adaptation for a=${test} #521

youkpan opened this issue May 5, 2024 · 7 comments

Comments

@youkpan
Copy link

youkpan commented May 5, 2024

e.g :

test = "test"
a=`${test}`

it's okay using browser debug. but not otto

@youkpan
Copy link
Author

youkpan commented May 5, 2024

I wrote a func:

func replace_val_with_js_eval(prompt string, js_vm *otto.Otto) string {
	re := regexp.MustCompile(`\$\{([^\}]+)\}`)
	// 使用FindAllStringSubmatch查找所有匹配项
	matches1 := re.FindAllStringSubmatch(prompt, -1)
	// 遍历匹配结果,提取变量名
	for _, match := range matches1 {
		if len(match) > 1 {
			//fmt.Println("找到变量:", match[1])
			result, err := js_vm.Run(match[1])
			if err == nil {
				if result.IsUndefined() {
					continue
				}
				if result.IsString() {
					val_s, _ := result.ToString()
					prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", val_s)
				} else if result.IsNumber() {
					val_n, _ := result.ToFloat()
					prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", g.Ftos(val_n))
				} else if result.IsBoolean() {
					val_b, _ := result.ToBoolean()
					if val_b {
						prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", "true")
					} else {
						prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", "false")
					}
				} else if result.IsNull() {
					prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", "null")
				} else if result.IsNaN() {
					prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", "NaN")
				} else if result.IsObject() {
					json, err := result.MarshalJSON()
					if err == nil {
						prompt = s.ReplaceAll(prompt, "${"+match[1]+"}", string(json))
					}

				}
			}
		}
	}
	return prompt
}

@chrislowth
Copy link

chrislowth commented May 5, 2024 via email

@youkpan
Copy link
Author

youkpan commented May 5, 2024

oh ,Thanks. why my browser sometimes support ES 6

@Asday
Copy link

Asday commented May 5, 2024

Because the browser developers can choose what to support.

@stevenh
Copy link
Collaborator

stevenh commented May 5, 2024

Happy to accept PRs to add ES6 support

@chrislowth
Copy link

chrislowth commented May 6, 2024 via email

@stevenh
Copy link
Collaborator

stevenh commented May 6, 2024

Individual PRs are preferred as that makes it much easier to review.

There's already a few pieces in there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants