Hello, i’m getting a runtime error when running ja...
# javascript
i
Hello, i’m getting a runtime error when running javascript converted from Kotlin/js,
TypeError: this.y1d_1.n is not a function
details in the thread ->
javascript:
Copy code
protoOf($initializeCOROUTINE$4).ta = function () {
    var suspendResult = this.ia_1;
    $sm: do
      try {
        var tmp = this.ga_1;
        switch (tmp) {
          case 0:
            this.ha_1 = 5;
            var this_0 = Companion_getInstance_7().d1d_1;
            if (KmLogging_getInstance().er_1) {
              var tmp_0 =  true ? this_0.jr_1 : 0;
              this_0.kr(tmp_0, toString_0('Initializing vfs rast manager'));
            }

            this.ga_1 = 1;
            suspendResult = await_0(_get_getAllFilePaths__ucd41i_0(this.x1d_1).t1c(), this);
            if (suspendResult === get_COROUTINE_SUSPENDED()) {
              return suspendResult;
            }

            continue $sm;
          case 1:
            this.y1d_1 = suspendResult;
            this.z1d_1 = this.y1d_1.n(); <-- error here
            this.ga_1 = 2;
            continue $sm;
          case 2:
            if (!this.z1d_1.o()) {
              this.ga_1 = 4;
              continue $sm;
            }

            this.a1e_1 = this.z1d_1.p();
            var tmp_1 = this;
            tmp_1.b1e_1 = this.a1e_1;
            this.ga_1 = 3;
            suspendResult = this.x1d_1.c1e(this.b1e_1, this);
            if (suspendResult === get_COROUTINE_SUSPENDED()) {
              return suspendResult;
            }

            continue $sm;
          case 3:
            this.ga_1 = 2;
            continue $sm;
          case 4:
            return Unit_instance;
          case 5:
            throw this.ja_1;
        }
      } catch ($p) {
        var e = $p;
        if (this.ha_1 === 5) {
          throw e;
        } else {
          this.ga_1 = this.ha_1;
          this.ja_1 = e;
        }
      }
     while (true);
  };
corresponding kotlin code (I think):
Copy code
fun interface GetAllFilePaths {
    operator fun invoke(): Promise<List<String>>
}

class VfsRastManager : ProjectService {
    override val project: Project by inject()
    private val getAllFilePaths: GetAllFilePaths by inject()
    private val getRastForPath: GetRastForPath by inject()
    private val fileToRastMap = mutableMapOf<String, VirtualFile>()

    operator fun get(uri: String): VirtualFile? = fileToRastMap[uri]

    suspend fun fetchAndStore(path: String) {
        val rast = getRastForPath(path).await()
        LOG.d { "Storing rast for ${path.lastPartAfterSlash}" }
        fileToRastMap[path] = VirtualFile(path, rast.asRast())
    }

    override suspend fun initialize() {
        LOG.d { "Initializing vfs rast manager" }
        getAllFilePaths().await().forEach { uri ->
            fetchAndStore(uri)
        }
    }
...
}
where the getAllFilePaths is implemented by a TS function:
Copy code
function getAllRustFiles(): Promise<string[]> {
    return vscode.workspace.findFiles('**/*.rs').then(rustFileUris => {
        console.debug('Getting all Rust files');
        return rustFileUris.map(uri => {
            console.debug(`Found Rust file: ${path.basename(uri.fsPath)}`);
            return uri.toString();
        });
    }) as Promise<string[]>;
}
value of
this.y1d_1
at runtime:
Copy code
[
0:"file:///myfilepath/myfile.rs",
...//and many more
]
[[Prototype]]:Array(0)
[[Prototype]]:Object
I guess it’s trying to get the length of the array using
.n()
but it doesn’t exist
e
So
getAllFilePaths
is an
external
Kotlin declaration? Strange that it gets renamed to
_get_getAllFilePaths__ucd41i_0
Or
getAllFilePaths
internally calls the TS function?
i
I think i’m handling all the coroutines/js async await thing wrong, i’m refactoring it now, will report back
e
Yeah I'm missing how you call that TS (JS) function tbh
For example, I don't understand the relation with
Copy code
fun interface GetAllFilePaths {
    operator fun invoke(): Promise<List<String>>
}
i
First time doing kotlin/js (or almost any JS for that matter) 🤷
e
Oh it's ok. Are you integrating Kotlin into a VS Code extension?
i
yep
to share logic with an IJ plugin
e
We do the same more or less. I'll ping you in PM later tonight.
i
Ah ok cool!
e
Just to close this one, the issue was with the use of
List
instead of
Array
to interface with JS.