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

1、EM_ASM方法

test.cpp

#include <pthread.h>
#include <emscripten.h>
#include <stdio.h>


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*)&times);
	    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

<!doctype html>
<html>
<head>
 
</head>
<body>
 <div class="items">
    <button type="button" onClick="start()">start</button>
</div>
 <script async type="text/javascript" src="test.js"></script>
 <script>
	function start()
    {
      let res= Module._test();
      console.log("res EM_ASM_INT:"+res);
    }
	function callRender(nu,size,m)
    {
      console.log("EM_ASM_INT callRender p1:"+nu+",p2:"+size+",p3:"+m);
	  console.log("test1.html 11111111166666661");
    }
 </script>

</body>
</html>

 2、EM_JS方法

test.cpp

#include <pthread.h>
#include <emscripten.h>
#include <stdio.h>

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*)&times);
	    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

<!doctype html>
<html>
<head>
 
</head>
<body>
 <div class="items">
    <button type="button" onClick="start()">start</button>
</div>
 <script async type="text/javascript" src="test.js"></script>
 <script>
	function start()
    {
      let res= Module._test();
      console.log("res EM_JS:"+res);
    }
	function callRender(nu,size,m)
    {
      console.log("callRender p1:"+nu+",p2:"+size+",p3:"+m);
	  console.log("test1.html 11111111166666661");
    }
 </script>

</body>
</html>

3、EMSCRIPTEN_RUN_SCRIPT方法

test.cpp


#include <pthread.h>
#include <emscripten.h>
#include <stdio.h>

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*)&times);
	    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

<!doctype html>
<html>
<head>
 
</head>
<body>
 <div class="items">
    <button type="button" onClick="start()">start</button>
</div>
 <script async type="text/javascript" src="test.js"></script>
 <script>
	function start()
    {
      let res= Module._test();
      console.log("res EMSCRIPTEN_RUN_script:"+res);
    }
	function callRender(nu,size,m)
    {
      console.log("callRender p1:"+nu+",p2:"+size+",p3:"+m);
	  console.log("11111111166666661");
    }
 </script>

</body>
</html>

4、在JS中实现C++ API

test.cpp

#include <stdio.h>
#include <pthread.h>
#include <emscripten.h>

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*)&times);
	    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

<!doctype html>
<html>
<head>
 
</head>
<body>
 <div class="items">
    <button type="button" onClick="start()">start</button>
</div>
 <script async type="text/javascript" src="test.js"></script>
 <script>
	function start()
    {
      let res= Module._test();
      console.log("res JS c++ API:"+res);
    }
	function callBuffer(iSize)
    {
      console.log("buffer size :"+iSize);
    }
 </script>

</body>
</html>

编译脚本

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

5、MAIN_THREAD_EM_ASM

void FrameInfoCallBack(int nPort,char * pBuf,int nSize,int nWidth,int nHeight,int nStamp,int nType,int nUser)
{
	MAIN_THREAD_EM_ASM({
		JSPlayM4_FrameCallBack($0,$1,$2,$3,$4);
	},nPort,(void*)pBuf,nSize,nStamp,nUser);
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值