[go: up one dir, main page]

File: test.c

package info (click to toggle)
duktape 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,496 kB
  • sloc: ansic: 203,676; python: 5,856; makefile: 476; cpp: 205
file content (56 lines) | stat: -rw-r--r-- 1,572 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include "duktape.h"
#include "duk_logging.h"

static duk_ret_t init_logging(duk_context *ctx, void *udata) {
	(void) udata;
	duk_logging_init(ctx, 0 /*flags*/);
	printf("top after init: %ld\n", (long) duk_get_top(ctx));

	/* C API test */
	duk_eval_string_noresult(ctx, "Duktape.Logger.clog.l = 0;");
	duk_log(ctx, DUK_LOG_TRACE, "c logger test: %d", 123);
	duk_log(ctx, DUK_LOG_DEBUG, "c logger test: %d", 123);
	duk_log(ctx, DUK_LOG_INFO, "c logger test: %d", 123);
	duk_log(ctx, DUK_LOG_WARN, "c logger test: %d", 123);
	duk_log(ctx, DUK_LOG_ERROR, "c logger test: %d", 123);
	duk_log(ctx, DUK_LOG_FATAL, "c logger test: %d", 123);
	duk_log(ctx, -1, "negative level: %s %d 0x%08lx", "arg string", -123, 0xdeadbeefUL);
	duk_log(ctx, 6, "level too large: %s %d 0x%08lx", "arg string", 123, 0x1234abcdUL);

	return 0;
}

int main(int argc, char *argv[]) {
	duk_context *ctx;
	int i;
	int exitcode = 0;

	ctx = duk_create_heap_default();
	if (!ctx) {
		return 1;
	}

	(void) duk_safe_call(ctx, init_logging, NULL, 0, 1);
	printf("logging init: %s\n", duk_safe_to_string(ctx, -1));
	duk_pop(ctx);

	for (i = 1; i < argc; i++) {
		printf("Evaling: %s\n", argv[i]);
		duk_push_string(ctx, argv[i]);
		duk_push_string(ctx, "evalCodeFileName");  /* for automatic logger name testing */
		if (duk_pcompile(ctx, DUK_COMPILE_EVAL) != 0) {
			exitcode = 1;
		} else {
			if (duk_pcall(ctx, 0) != 0) {
				exitcode = 1;
			}
		}
		printf("--> %s\n", duk_safe_to_string(ctx, -1));
		duk_pop(ctx);
	}

	printf("Done\n");
	duk_destroy_heap(ctx);
	return exitcode;
}