What happened to the Emscripten branch?

Emscripten, WASM, and asm.js related questions
Post Reply
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

I have read that workers in JavaScript can be used only with an HTTPS connection, with SSL certificates. Since threads require the use of workers, under Emscripten, that could be the reason why it doesn't work
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

Hi all!

I decided to change one program. I assembled it without errors, but got an exception in the browser. In the console the following:
GET
http://localhost:8000/favicon.ico
[HTTP/1 404 File not found 0ms]

wasm streaming compile failed: TypeError: WebAssembly: Response has unsupported MIME type 'application/octet-stream' expected 'application/wasm' mancala.html:138:19
printErr http://localhost:8000/mancala.html:138
instantiateAsync http://localhost:8000/mancala.js:1775
(Asynchronous: promise callback)
instantiateAsync http://localhost:8000/mancala.js:1772
(Asynchronous: promise callback)
instantiateAsync http://localhost:8000/mancala.js:1770
createWasm http://localhost:8000/mancala.js:1798
<anonymous> http://localhost:8000/mancala.js:7508
falling back to ArrayBuffer instantiation mancala.html:138:19
printErr http://localhost:8000/mancala.html:138
instantiateAsync http://localhost:8000/mancala.js:1776
(Asynchronous: promise callback)
instantiateAsync http://localhost:8000/mancala.js:1772
(Asynchronous: promise callback)
instantiateAsync http://localhost:8000/mancala.js:1770
createWasm http://localhost:8000/mancala.js:1798
<anonymous> http://localhost:8000/mancala.js:7508
..........
Uncaught RuntimeError: index out of bounds
x http://localhost:8000/mancala.js:7241
handleSleep http://localhost:8000/mancala.js:7326
safeSetTimeout http://localhost:8000/mancala.js:4719
setTimeout handler*safeSetTimeout http://localhost:8000/mancala.js:4716
_emscripten_sleep http://localhost:8000/mancala.js:7040
handleSleep http://localhost:8000/mancala.js:7305
_emscripten_sleep http://localhost:8000/mancala.js:7039
x http://localhost:8000/mancala.js:7217
x http://localhost:8000/mancala.js:7241
handleSleep http://localhost:8000/mancala.js:7326
safeSetTimeout http://localhost:8000/mancala.js:4719
setTimeout handler*safeSetTimeout http://localhost:8000/mancala.js:4716
_emscripten_sleep http://localhost:8000/mancala.js:7040
handleSleep http://localhost:8000/mancala.js:7305
_emscripten_sleep http://localhost:8000/mancala.js:7039
x http://localhost:8000/mancala.js:7217
x http://localhost:8000/mancala.js:7241
handleSleep http://localhost:8000/mancala.js:7326
safeSetTimeout http://localhost:8000/mancala.js:4719
setTimeout handler*safeSetTimeout http://localhost:8000/mancala.js:4716
_emscripten_sleep http://localhost:8000/mancala.js:7040
handleSleep http://localhost:8000/mancala.js:7305
_emscripten_sleep http://localhost:8000/mancala.js:7039
x http://localhost:8000/mancala.js:7217
x http://localhost:8000/mancala.js:7241
handleSleep http://localhost:8000/mancala.js:7326
safeSetTimeout http://localhost:8000/mancala.js:4719
setTimeout handler*safeSetTimeout http://localhost:8000/mancala.js:4716
_emscripten_sleep http://localhost:8000/mancala.js:7040
handleSleep http://localhost:8000/mancala.js:7305
_emscripten_sleep http://localhost:8000/mancala.js:7039
x http://localhost:8000/mancala.js:7217
x http://localhost:8000/mancala.js:7241
handleSleep http://localhost:8000/mancala.js:7326
safeSetTimeout http://localhost:8000/mancala.js:4719
setTimeout handler*safeSetTimeout http://localhost:8000/mancala.js:4716
_emscripten_sleep http://localhost:8000/mancala.js:7040
This program: https://users.freebasic-portal.de/freeb ... ncala.html
I build it like this: fbc -target js-asmjs mancala.bas -Wl "-s ASYNCIFY=1" -Wl --preload-file,res

Previously, I compiled on compiler 1.07 or 1.08 (the compiler compiled itself from source codes, as recommended by Victor in the 2015 year) and there were no such problems.
What could be wrong?
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

The problem is with the array index, which for some reason seems to be out of bounds. But I looked at the code bas and it seems to me that everything is fine there. Could there be an error in the conversion to Emscripten code?
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

I don't know what's the matter. But it doesn't work like this:

Code: Select all

	For i As Long = 0 To 5

		s = Str(bDimMancala(i+1).bValue)

		Var s2 = Str(bDimMancala(13-i).bValue)
		
		....

	Next
And this is how it works:

Code: Select all

	For i As Long = 0 To 5

		dim as long iOffset1 , iOffset2

		iOffset1 = i+1
		
		iOffset2 = 13 - i

		s = Str(bDimMancala(iOffset1).bValue)

		Var s2 = Str(bDimMancala(iOffset2).bValue)
		
		....

	Next
There is some problem in the compiler.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

Where is the file mancala.bas? In the archive I found only the files mancala(32-bit).bas and mancala(64-bit).bas , and they both are unusable, since they contain some ASM code.

Also, can you still compile it if you use an older version of FreeBasic? Because I wonder if the change happened in FreeBasic or in Emscripten
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

angros47 wrote: Sep 18, 2023 17:39 Where is the file mancala.bas? In the archive I found only the files mancala(32-bit).bas and mancala(64-bit).bas , and they both are unusable, since they contain some ASM code.

Also, can you still compile it if you use an older version of FreeBasic? Because I wonder if the change happened in FreeBasic or in Emscripten
Everything worked well with the version that was not yet built into the official compiler. That is, the Victor version, but corrected by you.
Where is the file mancala.bas? In the archive I found only the files mancala(32-bit).bas and mancala(64-bit).bas , and they both are unusable, since they contain some ASM code.
https://dropmefiles.com/2FaWO

Procedure DrawNumbers() , lines starting with 261 (loop). It's already fixed there, but you can return the error, as I indicated in my last post.
It looks like the compiler is incorrectly calculating the offset in parentheses for the Emscripten backend.

This is incorrect:

Code: Select all

bDimMancala(13-i).bValue
And this is what he thinks is correct:

Code: Select all

dim as long iOffset2
iOffset2 = 13 - i
bDimMancala(iOffset2).bValue
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

That's strange: I changed the code from:

Code: Select all

		iOffset1 = i+1
		
		iOffset2 = 13 - i

		s = Str(bDimMancala(iOffset1).bValue)

		Var s2 = Str(bDimMancala(iOffset2).bValue)
to:

Code: Select all

		iOffset1 = i+1
		
		iOffset2 = 13 - i

		s = Str(bDimMancala(i+1).bValue)

		Var s2 = Str(bDimMancala(13-i).bValue)
Indeed it crashes on the start, but I compared the C files, and this is the diff file:

Code: Select all

298c298
< 			FBSTRING* vr$26 = fb_IntToStr( (int32)*(int8*)(((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)I$2 * 49ll)) + 49ll) );
---
> 			FBSTRING* vr$26 = fb_IntToStr( (int32)*(int8*)((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)IOFFSET1$3 * 49ll)) );
301,304c301,304
< 			FBSTRING* vr$32 = fb_IntToStr( (int32)*(int8*)(((int64)(struct $8TMANCALA*)BDIMMANCALA$ + (-(int64)I$2 * 49ll)) + 637ll) );
< 			fb_StrInit( (void*)&S2$3, -1ll, (void*)vr$32, -1ll, 0 );
< 			int64 vr$35 = fb_StrLen( (void*)&S$1, -1ll );
< 			if( vr$35 != 2ll ) goto label$51;
---
> 			FBSTRING* vr$31 = fb_IntToStr( (int32)*(int8*)((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)IOFFSET2$3 * 49ll)) );
> 			fb_StrInit( (void*)&S2$3, -1ll, (void*)vr$31, -1ll, 0 );
> 			int64 vr$34 = fb_StrLen( (void*)&S$1, -1ll );
> 			if( vr$34 != 2ll ) goto label$51;
314,315c314,315
< 			int64 vr$47 = fb_StrLen( (void*)&S2$3, -1ll );
< 			if( vr$47 != 2ll ) goto label$53;
---
> 			int64 vr$46 = fb_StrLen( (void*)&S2$3, -1ll );
> 			if( vr$46 != 2ll ) goto label$53;
Basically, the code is equivalent, and some minor variables have changed number, nothing more. Original C file uses:

Code: Select all

			IOFFSET1$3 = (int32)((int64)I$2 + 1ll);
			IOFFSET2$3 = (int32)(-(int64)I$2 + 13ll);
			FBSTRING* vr$26 = fb_IntToStr( (int32)*(int8*)((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)IOFFSET1$3 * 49ll)) );
			fb_StrAssign( (void*)&S$1, -1ll, (void*)vr$26, -1ll, 0 );
			FBSTRING S2$3;
			FBSTRING* vr$31 = fb_IntToStr( (int32)*(int8*)((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)IOFFSET2$3 * 49ll)) );
the not working one uses:

Code: Select all

			IOFFSET1$3 = (int32)((int64)I$2 + 1ll);
			IOFFSET2$3 = (int32)(-(int64)I$2 + 13ll);
			FBSTRING* vr$26 = fb_IntToStr( (int32)*(int8*)(((int64)(struct $8TMANCALA*)BDIMMANCALA$ + ((int64)I$2 * 49ll)) + 49ll) );
			fb_StrAssign( (void*)&S$1, -1ll, (void*)vr$26, -1ll, 0 );
			FBSTRING S2$3;
			FBSTRING* vr$32 = fb_IntToStr( (int32)*(int8*)(((int64)(struct $8TMANCALA*)BDIMMANCALA$ + (-(int64)I$2 * 49ll)) + 637ll) );
They should be perfectly equivalent
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

If you compile under a regular gcc backend, the compiler first forcibly creates a temporary variable in which it performs the calculation in parentheses. And then this variable is placed in brackets. But for the Emscripten backend, the compiler does not do this and substitutes numbers. Perhaps this is the problem.

Also, I noticed that it is impossible to compile Emscripten backend with the -exx option, errors appear. Is this how it should be?
Xusinboy Bekchanov
Posts: 782
Joined: Jul 26, 2018 18:28

Re: What happened to the Emscripten branch?

Post by Xusinboy Bekchanov »

I also found a bug regarding WString size in WASM. In WASM, the size of WString must be twice as large as in Windows. Otherwise the text in the variable will be damaged. So I decided to do this in my library:

Code: Select all

#ifdef __USE_WASM__
	#define GrowLength 2
#else
	#define GrowLength 1
#endif

m_BytesCount = (m_Length + 1) * SizeOf(WString) * GrowLength
VANYA wrote: Sep 20, 2023 3:42 Also, I noticed that it is impossible to compile Emscripten backend with the -exx option, errors appear. Is this how it should be?
clang does not support getting the address of line numbers. That is, commands like Resume Next will not work.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

VANYA wrote: Sep 20, 2023 3:42 If you compile under a regular gcc backend, the compiler first forcibly creates a temporary variable in which it performs the calculation in parentheses. And then this variable is placed in brackets. But for the Emscripten backend, the compiler does not do this and substitutes numbers. Perhaps this is the problem.
I don't know why, since it seems legit C syntax

Also, I have a question: does the INPUT work in graphic mode, compiled with ASYNCIFY? In the old version, I saw it asked something before starting, and now it doesn't...
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

Xusinboy Bekchanov wrote: Sep 20, 2023 7:15 clang does not support getting the address of line numbers. That is, commands like Resume Next will not work.
Thank you for the clarification!
Also, I have a question: does the INPUT work in graphic mode, compiled with ASYNCIFY?
work.
I don't know why, since it seems legit C syntax
Here is a minimal example that leads to an exception:

Code: Select all

ScreenRes 792,400,32,2

Dim Shared As long bDimMancala(13)

Sub DrawNumbers()

	For i As Long = 0 To 5
		
		bDimMancala(13 - i) = i
		
	next

End Sub

DrawNumbers()
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

I can see that:

Code: Select all

Dim shared As long bDimMancala(13)


Sub DrawNumbers()

	For i As Long = 0 To 5
		
		bDimMancala(13 - i) = i
		
	next

End Sub

DrawNumbers()
produces exception, while:

Code: Select all


Sub DrawNumbers()
Dim As long bDimMancala(13)

	For i As Long = 0 To 5
		
		bDimMancala(13 - i) = i
		
	next

End Sub

DrawNumbers()
works fine. So, it affects only shared arrays...
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What happened to the Emscripten branch?

Post by angros47 »

Ok, now I have found a minimal code written in C to trigger the issue:

This code:

Code: Select all

int main(){
	int arr[13];
	for (int i=0;i<=5;i++){
		*(int*)(((int)(int*)arr + (-i << (2 & 31))) + 52) = i;
	}
}
compiled with "emcc bug.c -o bug.html" works flawlessly

Code: Select all

int arr[13];
int main(){
	for (int i=0;i<=5;i++){
		*(int*)(((int)(int*)arr + (-i << (2 & 31))) + 52) = i;
	}
}
that should be equivalent (the only difference is that the array is shared) crashes. Can you confirm it?
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: What happened to the Emscripten branch?

Post by adeyblue »

Those write out of bounds on the first loop (arr + 52 writes to arr[13]). If it still happens with +48 instead of +52 then that's a worry.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: What happened to the Emscripten branch?

Post by VANYA »

that should be equivalent (the only difference is that the array is shared) crashes. Can you confirm it?
yes.
Post Reply