Skip to content

Instantly share code, notes, and snippets.

@kinjalkishor
Last active December 20, 2025 21:28
Show Gist options
  • Select an option

  • Save kinjalkishor/be8fab9bd25e2fff4ff043d37e29a9cb to your computer and use it in GitHub Desktop.

Select an option

Save kinjalkishor/be8fab9bd25e2fff4ff043d37e29a9cb to your computer and use it in GitHub Desktop.
#if _MSC_VER
#pragma comment(linker, "/subsystem:console")
#endif
//--------------------------
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#pragma comment (lib ,"wrench.lib")
#include <wrench.h>
#include <string.h>
void print( WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr )
{
char buf[1024];
for( int i=0; i<argn; ++i )
{
printf( "%s", argv[i].asString(buf, 1024) );
}
}
void mul_cpp( WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr )
{
int a = argv[0].asInt();
int b = argv[1].asInt();
retVal.setInt(a * b);
}
const char* wrenchCode = R"(
function add(a, b) {
a = mul_cpp(a, b);
print(a, ", ", b, "\n");
//print(a); print(", "); print(b); print("\n");
return a + b;
}
print( "Hello World from wrench\n" );
for( var i=0; i<10; i++ ) {
print(i);
}
print("\n");
)";
int main( int argn, char** argv )
{
WRState* w = wr_newState(); // create the state
wr_registerFunction( w, "print", print ); // bind a function
wr_registerFunction( w, "mul_cpp", mul_cpp );
unsigned char* outBytes; // compiled code is alloc'ed
int outLen;
int err = wr_compile( wrenchCode, strlen(wrenchCode), &outBytes, &outLen ); // compile it
WRContext* m_context = nullptr;
if ( err == 0 )
{
m_context = wr_run( w, outBytes, outLen, true ); // load and run the code!
}
// How to send arguments to add.
WRFunction* m_func = wr_getFunction(m_context, "add");
int num_args = 2;
WRValue args[2];
args[0].setInt(4);
args[1].setInt(5);
WRValue* returnValue = wr_callFunction( m_context, m_func, args, num_args );
int result = returnValue->asInt();
printf("Result of add function from wrench: %d\n", result);
//------------------------------
//---------------------
wr_destroyState( w );
//------------------------
printf("\n");
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment