File: test_script.c

package info (click to toggle)
fungw 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,268 kB
  • sloc: ansic: 24,257; makefile: 500; sh: 32; awk: 9; perl: 8; tcl: 7; javascript: 7; ruby: 7; python: 6
file content (80 lines) | stat: -rw-r--r-- 2,564 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <stdio.h>
#include <libfungw/fungw.h>

/* User configuration for this example: change language and script file name */
#define SCLANG      fawk
#define SCLANG_STR  "fawk"
#define SCRIPT_FN   "../00_hello/hello.fawk"

/* Macro wizardy for making the example language-independent */
#define SCLANG_INIT_(LANG) pplg_init_fungw_ ## LANG ()
#define SCLANG_INIT(LANG) SCLANG_INIT_(LANG)
extern int SCLANG_INIT(SCLANG);

/* Handle runtime fungw errors - inform the user */
void async_error(fgw_obj_t *obj, const char *msg)
{
	printf("Async error: %s: %s\n", obj->name, msg);
}

/* Script calls C function: wrapper for atoi() */
fgw_error_t fgwc_atoi(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	FGW_DECL_CTX;                        /* setup for the FGW macro shorthands, initializes fgw_ctx_t *ctx */
	FGW_ARGC_REQ_MATCH(1);               /* require exactly one argument */
	FGW_ARG_CONV(&argv[1], FGW_STR);     /* make sure argv[1] is a string */

	/* call the function, prepare the result in res */
	res->val.nat_int = atoi(argv[1].val.str);
	res->type = FGW_INT;
	return FGW_SUCCESS;
}

int main(int argc, char *argv[])
{
	fgw_ctx_t ctx;
	fgw_obj_t *hobj, *sobj;
	fgw_arg_t res;
	int rv;

	/* Initialize the language engine; in a more advanced setup the .so is
	   loaded using dlopen() which automates this step; in a static link
	   setup, like this example, the script lang engine needs to be linked and
	   initialized manually. */
	SCLANG_INIT(SCLANG);

	/* initialize a context */
	fgw_init(&ctx, "my_context");
	ctx.async_error = async_error;

	/* create a host object and register our own functions there */
	hobj = fgw_obj_reg(&ctx, "host_app");
	fgw_func_reg(hobj, "atoi", fgwc_atoi);

	/* create objects (load script) */
	sobj = fgw_obj_new(&ctx, "hello", SCLANG_STR, SCRIPT_FN, NULL);
	if (sobj == NULL) {
		fprintf(stderr, "ERROR: failed to load script %s (language %s)\n", SCRIPT_FN, SCLANG_STR);
		return 1;
	}

	/* Call a script function */
	printf("\n### Call hello()\n");
	rv = fgw_vcall(&ctx, &res, "hello",   FGW_INT, (int)12,   FGW_STR, "blobbs",   0);
	if (rv == 0) {
		int cr = fgw_arg_conv(&ctx, &res, FGW_INT); /* convert result to int if it is not an int already */
		if (cr == 0)
			printf("hello() result=%d\n", res.val.nat_int);
		else
			printf("hello() result=<conversion failure>\n");
	}
	else
		fprintf(stderr, "ERROR: calling hello() failed\n");


	fgw_uninit(&ctx);   /* uninit the context; new contexts could still be created */
	fgw_atexit();       /* free all memory used by the lib */

	return 0;
}