te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Can I call a c++ function from JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I call a c++ function from JavaScript? - Stack Overflow

programmeradmin4浏览0评论

There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.

There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.

Share Improve this question edited Jul 15, 2022 at 20:41 AndrewL64 16.3k8 gold badges50 silver badges85 bronze badges asked May 7, 2015 at 12:32 AyanAyan 1631 silver badge7 bronze badges 9
  • 8 No, it is not possible. – RevanProdigalKnight Commented May 7, 2015 at 12:33
  • From an native-enabled Extension, perhaps, otherwise No. – Alex K. Commented May 7, 2015 at 12:35
  • 2 Short answer: usually. It depends on the javascript engine built into the browser. Each will have it's own way of doing things (consult developer docs). – Richard Hodges Commented May 7, 2015 at 12:39
  • 7 This sounds like a security catastrophe waiting to happen. – Joost Commented May 7, 2015 at 12:45
  • 1 If Chrome-only is fine, have a look at PNaCl – gd1 Commented Aug 26, 2015 at 22:04
 |  Show 4 more ments

4 Answers 4

Reset to default 2

A few options I can think of:

1) Find a different library in JavaScript.

2) Port the C++ code over to JavaScript. How easy this is depends on how plex the C++ code is or how much of it you need.

3) Wrap the C++ code in a web service on a server and call it using AJAX. I'm not personally familiar with any C++ web service frameworks, but barring that, you could create Java, Python, or .NET bindings for the library (or just for the portion that you need), or bindings in another language. Then you would write your web service in whatever language you chose.

3B) Alternative to #3 - If the library has a mand-line interface, or if there exists a mand-line program that uses the library, your web service could call that and you wouldn't have to write a language binding. Note however that there are performance & security problems to be aware of with this option.

4) If you have the C++ source code for the library, you could try to pile the library to JavaScript using Emscripten.

I would probably try those in that order, roughly. I might try #3 last cause it could be the trickiest.

Also, if you do #3 or #3B, you'll want to be sure your use of the library is thread-safe.

Disclaimer: I've never tried any of these except #3B.

You'd be better off creating a 'C' wrapper for the C++ library and calling that from the Javascript.

C++ is notoriously difficult to interface with due to the language standard lacking a tight ABI definition. C does not have this limitation. For this reason, 'C' ends up being the lingua franca when you want two programming languages to talk with each other.

Usually browsers doesn't allow that, because that's very insecure. You might pile C++ in asm.js and use it as JavaScript library.

Alternatively you can create browser extension, which will run or call desired code.

Yes its possible but before you can call them from javascript. You do this by creating an interface file to tell javascript about the interface.

发布评论

评论列表(0)

  1. 暂无评论