Open youkpan opened 4 months ago
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
}
OTTO implements the EMCA 5 (ES 5) standard.
Template literals (back-tick strings) are not part of that standard - they were introduced in ES 6 and so are not supported by OTTO.
On Sun, 5 May 2024 at 07:49, yq-pan @.***> wrote:
e.g : test = "test" a=${test} it's okay using browser debug. but not otto
— Reply to this email directly, view it on GitHub https://github.com/robertkrimen/otto/issues/521, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH2YHJIFUHL47HT6LHFM7MTZAXI6RAVCNFSM6AAAAABHHPBGYOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI3TSMZVGYYDAOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
oh ,Thanks. why my browser sometimes support ES 6
Because the browser developers can choose what to support.
Happy to accept PRs to add ES6 support
Interesting.
I added partial support for fat-arrow functions and template strings for a project with my previous employer. Sadly the code is theirs not mine and I no longer have access to it.
Would you accept individual ES6 features or are you looking for a complete upgrade?
Chris
On Sun, 5 May 2024 at 23:11, Steven Hartland @.***> wrote:
Happy to accept PRs to add ES6 support
— Reply to this email directly, view it on GitHub https://github.com/robertkrimen/otto/issues/521#issuecomment-2094969287, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH2YHJJOBXAYHOJZELYQCRLZA2U7HAVCNFSM6AAAAABHHPBGYOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJUHE3DSMRYG4 . You are receiving this because you commented.Message ID: @.***>
Individual PRs are preferred as that makes it much easier to review.
There's already a few pieces in there.
e.g :
it's okay using browser debug. but not otto