GithubHelp home page GithubHelp logo

Comments (2)

riicchhaarrd avatar riicchhaarrd commented on June 21, 2024 1

Edit

I misread your question, my mistake.
See https://github.com/riicchhaarrd/gsc/blob/master/src/standalone.c#L78

//example
se_addint(vm, 123);
se_addstring(vm, "test");
vm_exec_thread(vm, "your_script_routine", 2); //vm pointer, function name in script, number of arguments

//script function would look like this
your_script_routine(a, b)
{
//a would be test
//b would be 123
}

If you would like to do the opposite and expose C functions to the script, then read further below.

Writing glue code/wrapper

  1. Create your to be exported function
int my_function(vm_t *vm)
{
	int arg = se_getint(vm, 0); //get the first argument to my function from the stack
	
	//you can call your glue/wrapper c function here and return the value to gsc
	//e.g
	//se_addfloat(vm, sinf(se_getfloat(vm, 0));
	//return 1;
	
	if(arg == 1)
		se_addstring(vm, "my message"); //push a string onto the stack
	else if(arg == 2)
		se_addint(vm, 123); //we push integer 123 onto the stack
	else
		return 0; //return 0 means we don't have a return value and null will be pushed onto the stack
	return 1; //return 1 means we have a return value
}
  1. Add it to a array of type stockfunction_t
stockfunction_t my_function_set[] = {
	{"my_function", my_function},
	{NULL,NULL}
};
  1. After creating a vm instance (vm_t*) add it to the stock functions with se_register_stockfunction_set

Example of how to create a vm instance: (https://github.com/riicchhaarrd/gsc/blob/master/src/standalone.c#L73)

extern stockfunction_t my_function_set[];
se_register_stockfunction_set(vm, my_function_set);
  1. Run the VM and call your function
main()
{
	value = my_function(1);
	printf("value = " + value + "\n");
	value = my_function(2);
	printf("value = " + value + "\n");
	value = my_function(3);
	printf("value = " + value + "\n");
}

Alternative method using FFI

The exported function from the library has to be in cdecl calling convention and at the moment there's only support for x86.
Use #pragma comment(lib, "/path/to/library") with the path of the library

Example

Windows
#pragma comment(lib, "msvcrt.dll")

Linux
//replace the path with the path to your library that you want to import the C function from
#pragma comment(lib, "/lib/i386-linux-gnu/i686/cmov/libc.so.6")

#pragma comment(lib, "msvcrt.dll")

main()
{
	//When there's a conflict and printf is a builtin function of GSC and it exists in the library aswell, the GSC function will be used by default.
	//To combat this, if you put a dollar sign in front it will always call the exported function from the shared library.
	//e.g
	
	printf("Hello world\n"); //builtin function
	$printf("Hello world\n"); //FFI function
	
	//If the function isn't a builtin function, then omitting is fine too.
	i = atoi("12345");
	printf("i = %\n", i);

	p = malloc(1024);
	$snprintf(p,1024,"%d %s\n", i, "ok");
	$printf("%s", p);
	free(p);
}

from cgsc.

jarroddavis68 avatar jarroddavis68 commented on June 21, 2024

Ahh I see, wonderful thanks so much. Maybe make this info a wiki entry or something.

from cgsc.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.