ES6 Language Feature in Scripting / 我们需要 ES6 语言特性

中文

#lang:zh
SV Studio 的脚本引擎目前只支持 ES5.1 语言特性。这将为脚本的编写和调试带来一些不便和困难。当然,这个问题不像其他问题那么紧急,只是作为一个提议供大家讨论和参考。

letvar

当我在写一个需要遍历某个数组的脚本时,我使用了 let 关键字来在 for 循环中进行变量声明。由于我事先并不知道 SV Studio 的脚本引擎不支持 ES6 语言特性,因此编辑器在加载脚本时对我报错:“Parse Error at line x.”

注:后来我翻文档才知道只支持 5.1

我差不多用了两三分钟才排查出这个问题是由 let 关键字引起的。你可能会想,把它改成 var 就行了。事实上,对于这个脚本,问题确实解决了。

但是我们来看看下面这个例子:

(省略 getClientInfo() 部分)
function main() {
    var arr = [];
    for(var i=0; i<10; i++){
        arr [i] = function(){
            SV.showMessageBox("Title",i)
        }
    }
    arr [8](); 
}

运行结果为:
image

显然,这不是我们预期的结果。这个问题是由 var 的作用域引起的,会给程序的编写带来一定程度上的不便,也会给调试带来困难。

箭头函数 =>

箭头函数的使用可以简化代码、提升可读性。能支持当然是好的。

其他语言特性

其他语言特性在 SV Studio 的场景中提升似乎并不明显。此处不再赘述。

English

#lang:en
It seems that the scripting engine of SV Studio only supports ES5.1 language features, which will cause some inconvenience and difficulties for scripting and debugging. Certainly, this issue is not as urgent as other issues, and is just a suggestion for discussion and reference.

let and var

When I was writing a script that needed to go through an array, I used the let keyword to do the variable declaration in the for loop. Since I didn’t know that SV Studio’s scripting engine doesn’t support ES6 language features, the editor gave me an error when trying to load the script: “Parse Error at line x.”

P.S. I found it out in the docs that it only supports 5.1 later.

It took me about 2-3 minutes to figure out that the problem was caused by the let keyword. You might be thinking, just replace it with var and it will work. In fact, for this script, it did solve the problem.

But then let’s take a look at the following example.

(omitting the getClientInfo() part)
function main() {
    var arr = [];
    for(var i=0; i<10; i++){
        arr [i] = function(){
            SV.showMessageBox("Title",i)
        }
    }
    arr [8](); 
}

The running result is:
image

Obviously, this is not the result we expected. The problem is caused by the scope of var, which can cause some degree of inconvenience in writing the program and make debugging difficult.

The arrow function =>

The use of arrow functions can simplify code and improve readability. It is certainly great to have support for it.

Other language features

Other language features don’t seem to be a significant improvement in the SV Studio scenario. I won’t go into them here.

Translated with DeepL Translate: The world's most accurate translator (free version)

「いいね!」 1

I agree that, but it’s probably not possible because they didn’t develop the implementation themselves.
Recently, when I developed a script, I developed it with Typescript and compiled it with es5. I think this is one of the best alternatives for script developers who want the latest syntax.

「いいね!」 1

Nice suggestion.

In this case we may need a api.ts file to define the types and objects for eslint to check, and this seems to be possible and not difficult to make up.
Now on my to-do.

「いいね!」 2

By using " // @ts-ignore", you can bypass type checking for SynthV-specific functions too.

Example code

function main(){
    // @ts-ignore
    var selectedNotes = SV.getMainEditor().getSelection().getSelectedNotes();
    if (selectedNotes.length == 0) {
        // @ts-ignore
        SV.showMessageBox("Warning", "There is no Selected Notes.");
    } else {
        var original = '';
        for (var i=0;i<selectedNotes.length;i++) {
            var note = selectedNotes[i];
            original += note.getLyrics();
        }
        var prono = runKoG2P(original);
        var tokens = tokneizer(prono);

        for (var i=0;i<selectedNotes.length;i++) {
            var note = selectedNotes[i];
            var phone = ipa2svp(tokens[i]);
            note.setPhonemes(phone);
        }

    }
    // @ts-ignore
    SV.finish();
}
「いいね!」 1

Done a sv.d.ts here if you would like to see it.

「いいね!」 1