port some JavaScript to C++