C++调JS上层的4种方法

1、EM_ASM方法

test.cpp

#include 
#include 
#include 


extern "C"
{
	EMSCRIPTEN_KEEPALIVE
	void *decodeHEVC(void* time)
	{
		int *iter=(int*) time;
		for(int i=0;i<20;i++)
		{
			if(i%5==0)
			{
				printf("5 de beishu\n");
			}
		}
		printf("decodeHEVC\n");
		EM_ASM_INT({
			callRender($0,5,6);
			console.log('test.cpp EM_ASM_INT P:'+$0);
		},100 );//调用JS上层函数-》不可行
		return time;
	}
	
	EMSCRIPTEN_KEEPALIVE
	int test() {
		pthread_t timer_thread_sub;
		int times = 40;
	    int timerCreateRess=pthread_create(&timer_thread_sub,NULL,decodeHEVC,(void*)×);
	    printf("sun pthread_create:%d\n",timerCreateRess);
	    if(timerCreateRess)
		{
	     	perror("sub Thread create failed");
			return NULL;
		}
		int timerJoinRess=pthread_join(timer_thread_sub,NULL);
		printf("sun pthread_join:%d\n",timerJoinRess);	
		if(timerJoinRess)
		{
			perror("sub Thread join failed");
			return NULL;
		}
		printf("sub  fibonacci 40 : %d\n",times);
        //EM_ASM_INT({
		//	callRender($0,5,6);
		//	console.log('test.cpp EM_ASM_INT P:'+$0);
		//},100 );//调用JS上层函数-》不可行
        return 0;
   }
}

test.html




 


 

 2、EM_JS方法

test.cpp

#include 
#include 
#include 

EM_JS(void, call_alert, (int a, int b, int c), {
  callRender(a,b,c);
  console.log("test.cpp EM_JS,nihao!");
});

extern "C"
{
	EMSCRIPTEN_KEEPALIVE
	void *decodeHEVC(void* time)
	{
		int *iter=(int*) time;
		for(int i=0;i<20;i++)
		{
			if(i%5==0)
			{
				printf("5 de beishu\n");
			}
		}
		printf("decodeHEVC\n");
		call_alert(5,6,7);//调用JS上层函数-》不可行
		return time;
	}
	
	EMSCRIPTEN_KEEPALIVE
	int test() {
		pthread_t timer_thread_sub;
		int times = 40;
	    int timerCreateRess=pthread_create(&timer_thread_sub,NULL,decodeHEVC,(void*)×);
	    printf("sun pthread_create:%d\n",timerCreateRess);
	    if(timerCreateRess)
		{
	     	perror("sub Thread create failed");
			return NULL;
		}
		int timerJoinRess=pthread_join(timer_thread_sub,NULL);
		printf("sun pthread_join:%d\n",timerJoinRess);	
		if(timerJoinRess)
		{
			perror("sub Thread join failed");
			return NULL;
		}
		printf("sub  fibonacci 40 : %d\n",times);
        //call_alert(5,6,7);//可行
     return 0;
   }
}

test.html




 


 

3、EMSCRIPTEN_RUN_SCRIPT方法

test.cpp


#include 
#include 
#include 

extern "C"
{
	EMSCRIPTEN_KEEPALIVE
	void *decodeHEVC(void* time)
	{
		int *iter=(int*) time;
		for(int i=0;i<20;i++)
		{
			if(i%5==0)
			{
				printf("5 de beishu\n");
			}
		}
		printf("decodeHEVC\n");
		emscripten_run_script("callRender(3,4,56)");//回调JS上层方法
		return time;
	}
	
	EMSCRIPTEN_KEEPALIVE
	int test() {
		pthread_t *timer_thread_sub;
		int times = 40;
	    int timerCreateRess=pthread_create(&timer_thread_sub,NULL,decodeHEVC,(void*)×);
	    printf("sun pthread_create:%d\n",timerCreateRess);
	    if(timerCreateRess)
		{
	     	perror("sub Thread create failed");
			return NULL;
		}
		int timerJoinRess=pthread_join(timer_thread_sub,NULL);
		printf("sun pthread_join:%d\n",timerJoinRess);	
		if(timerJoinRess)
		{
			perror("sub Thread join failed");
			return NULL;
		}
		printf("sub  fibonacci 40 : %d\n",times);
        //emscripten_run_script("callRender(3,4,56)");
     return 0;
   }
}

test.html




 


 

4、在JS中实现C++ API

test.cpp

#include 
#include 
#include 

extern "C"
{
	extern void my_js(int size);
	
	EMSCRIPTEN_KEEPALIVE
	void *decodeHEVC(void* time)
	{
		int *iter=(int*) time;
		for(int i=0;i<20;i++)
		{
			if(i%5==0)
			{
				printf("5 de beishu\n");
			}
		}
		printf("decodeHEVC\n");
		my_js(505050505);//调用JS上层函数-》跨线程回调
		return time;
	}

	EMSCRIPTEN_KEEPALIVE
    int test() {
		pthread_t timer_thread_sub;
	    int times = 40;
	    int timerCreateRess=pthread_create(&timer_thread_sub,NULL,decodeHEVC,(void*)×);
	    printf("sun pthread_create:%d\n",timerCreateRess);
	    if(timerCreateRess)
		{
	     	perror("sub Thread create failed");
			return NULL;
		}
		int timerJoinRess=pthread_join(timer_thread_sub,NULL);
		printf("sun pthread_join:%d\n",timerJoinRess);	
		if(timerJoinRess)
		{
			perror("sub Thread join failed");
			return NULL;
		}
		printf("sub  fibonacci 40 : %d\n",times);
	    //my_js(5);//可行
	    return 0;
    }
	
}


testlibrary.js

mergeInto(LibraryManager.library, {
  my_js__proxy:'async',
  my_js__sig:'viii',
  my_js: function(iSize) {
	callBuffer(iSize);
    console.log('testlibrary.js isize:'+iSize);
  },
});

test.html




 


 

编译脚本

emcc test.cpp -o test.js -s EXPORTED_FUNCTIONS="['_test']" --js-library testlibrary.js  -s USE_PTHREADS=1  -s PTHREAD_POOL_SIZE=4

你可能感兴趣的:(javascript,c++,开发语言,wasm)