Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Dukboot
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
dukboot
Dukboot
Commits
16ef1e9c
Unverified
Commit
16ef1e9c
authored
May 14, 2017
by
Will Hilton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix the goddamn indentation (shudder)
parent
7202fbbf
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
82 deletions
+78
-82
.editorconfig
.editorconfig
+7
-0
src/main.c
src/main.c
+41
-43
src/native_adder.c
src/native_adder.c
+8
-8
src/native_import.c
src/native_import.c
+11
-19
src/native_print.c
src/native_print.c
+6
-6
src/register_globals.c
src/register_globals.c
+5
-6
No files found.
.editorconfig
0 → 100644
View file @
16ef1e9c
[*]
end_of_line = lf
insert_final_newline = true
[*.{c,h}]
indent_style = space
indent_size = 2
\ No newline at end of file
src/main.c
View file @
16ef1e9c
/*
* Very simple example program
*/
* Very simple example program
*/
#include <stdio.h>
#include <stdlib.h> // malloc
#include "duktape.h"
// @see [1]
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <io.h>
#include <fcntl.h>
#endif
#include "native_print.c"
...
...
@@ -17,48 +17,46 @@
#include "register_globals.c"
int
main
(
int
argc
,
char
*
argv
[])
{
duk_int_t
ret
=
0
;
if
(
argc
<
2
)
{
printf
(
"Usage: dukboots file-to-eval.js
\n
"
);
printf
(
"Yup. Pretty basic API at the moment.
\n
"
);
exit
(
0
);
}
// Re-open stdin and stdout in binary mode
freopen
(
NULL
,
"rb"
,
stdin
);
freopen
(
NULL
,
"wb"
,
stdout
);
// @see [1]
#ifdef _WIN32
_setmode
(
0
,
_O_BINARY
);
_setmode
(
1
,
_O_BINARY
);
#endif
duk_context
*
ctx
=
duk_create_heap_default
();
register_globals
(
ctx
);
ret
=
native_import
(
ctx
,
argv
[
1
]);
if
(
ret
!=
0
)
{
fprintf
(
stderr
,
"%s %s
\n
"
,
argv
[
1
],
duk_safe_to_string
(
ctx
,
-
1
));
goto
finally
;
if
(
argc
<
2
)
{
printf
(
"Usage: dukboots file-to-eval.js
\n
"
);
printf
(
"Yup. Pretty basic API at the moment.
\n
"
);
exit
(
0
);
}
ret
=
duk_pcall
(
ctx
,
0
);
if
(
ret
!=
0
)
{
fprintf
(
stderr
,
"%s %s
\n
"
,
argv
[
1
],
duk_safe_to_string
(
ctx
,
-
1
));
goto
finally
;
// Re-open stdin and stdout in binary mode
freopen
(
NULL
,
"rb"
,
stdin
);
freopen
(
NULL
,
"wb"
,
stdout
);
// @see [1]
#ifdef _WIN32
_setmode
(
0
,
_O_BINARY
);
_setmode
(
1
,
_O_BINARY
);
#endif
duk_int_t
ret
=
0
;
duk_context
*
ctx
=
duk_create_heap_default
();
register_globals
(
ctx
);
ret
=
native_import
(
ctx
,
argv
[
1
]);
if
(
ret
!=
0
)
{
fprintf
(
stderr
,
"%s %s
\n
"
,
argv
[
1
],
duk_safe_to_string
(
ctx
,
-
1
));
goto
finally
;
}
ret
=
duk_pcall
(
ctx
,
0
);
if
(
ret
!=
0
)
{
fprintf
(
stderr
,
"%s %s
\n
"
,
argv
[
1
],
duk_safe_to_string
(
ctx
,
-
1
));
goto
finally
;
}
finally:
duk_destroy_heap
(
ctx
);
return
ret
;
duk_destroy_heap
(
ctx
);
return
ret
;
}
/**
* Footnotes
*
* [1] These hacks are needed because Windows mutates files opened in text mode
* (which stdin and stdout are) by replacing all \n with \r\n. I want
* binary identical output regardless of the host environment dammit.
*/
* Footnotes
*
* [1] These hacks are needed because Windows mutates files opened in text mode
* (which stdin and stdout are) by replacing all \n with \r\n. I want
* binary identical output regardless of the host environment dammit.
*/
src/native_adder.c
View file @
16ef1e9c
static
duk_ret_t
native_adder
(
duk_context
*
ctx
)
{
int
i
;
int
n
=
duk_get_top
(
ctx
);
/* #args */
double
res
=
0
.
0
;
int
i
;
int
n
=
duk_get_top
(
ctx
);
/* #args */
double
res
=
0
.
0
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
res
+=
duk_to_number
(
ctx
,
i
);
}
for
(
i
=
0
;
i
<
n
;
i
++
)
{
res
+=
duk_to_number
(
ctx
,
i
);
}
duk_push_number
(
ctx
,
res
);
return
1
;
/* one return value */
duk_push_number
(
ctx
,
res
);
return
1
;
/* one return value */
}
src/native_import.c
View file @
16ef1e9c
int
native_import
(
duk_context
*
ctx
,
char
*
filename
)
{
FILE
*
pFile
;
long
fileSize
;
char
*
buffer
;
size_t
bytesRead
;
duk_int_t
ret
;
// Open the file
pFile
=
fopen
(
filename
,
"rb"
);
FILE
*
pFile
=
fopen
(
filename
,
"rb"
);
if
(
pFile
==
NULL
)
{
fputs
(
"File error
\n
"
,
stderr
);
exit
(
1
);
}
// Get file size http://www.cplusplus.com/reference/cstdio/fread/
fseek
(
pFile
,
0
,
SEEK_END
);
fileSize
=
ftell
(
pFile
);
long
fileSize
=
ftell
(
pFile
);
rewind
(
pFile
);
// Allocate a buffer the write size
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
fileSize
);
char
*
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
fileSize
);
if
(
buffer
==
NULL
)
{
fputs
(
"Memory error
\n
"
,
stderr
);
exit
(
2
);
}
// Dump file into memory
bytesRead
=
fread
(
buffer
,
1
,
fileSize
,
pFile
);
size_t
bytesRead
=
fread
(
buffer
,
1
,
fileSize
,
pFile
);
if
(
bytesRead
!=
fileSize
)
{
fputs
(
"Reading error
\n
"
,
stderr
);
exit
(
3
);
}
// Close the file bc we're done with it
fclose
(
pFile
);
//printf("Loaded %ld bytes from '%s'\n", fileSize, argv[1]);
// Create a function closure from the file buffer.
// Leaves it on the stack.
duk_push_string
(
ctx
,
filename
);
ret
=
duk_pcompile_lstring_filename
(
ctx
,
DUK_COMPILE_FUNCTION
+
DUK_COMPILE_STRICT
,
buffer
,
bytesRead
);
free
(
buffer
);
return
ret
;
}
\ No newline at end of file
// Create a function from the file buffer. Leaves it on the stack.
duk_int_t
ret
;
duk_push_string
(
ctx
,
filename
);
ret
=
duk_pcompile_lstring_filename
(
ctx
,
DUK_COMPILE_FUNCTION
+
DUK_COMPILE_STRICT
,
buffer
,
bytesRead
);
free
(
buffer
);
return
ret
;
}
src/native_print.c
View file @
16ef1e9c
static
duk_ret_t
native_print
(
duk_context
*
ctx
)
{
duk_push_string
(
ctx
,
" "
);
duk_insert
(
ctx
,
0
);
duk_join
(
ctx
,
duk_get_top
(
ctx
)
-
1
);
printf
(
"%s
\n
"
,
duk_safe_to_string
(
ctx
,
-
1
));
return
0
;
}
\ No newline at end of file
duk_push_string
(
ctx
,
" "
);
duk_insert
(
ctx
,
0
);
duk_join
(
ctx
,
duk_get_top
(
ctx
)
-
1
);
printf
(
"%s
\n
"
,
duk_safe_to_string
(
ctx
,
-
1
));
return
0
;
}
src/register_globals.c
View file @
16ef1e9c
// TODO: Can we remove this stuff once we have a global proxy object
// we pass into the top-level function?
void
register_globals
(
duk_context
*
ctx
)
{
duk_push_c_function
(
ctx
,
native_print
,
DUK_VARARGS
);
duk_put_global_string
(
ctx
,
"print"
);
duk_push_c_function
(
ctx
,
native_adder
,
DUK_VARARGS
);
duk_put_global_string
(
ctx
,
"adder"
);
}
\ No newline at end of file
duk_push_c_function
(
ctx
,
native_print
,
DUK_VARARGS
);
duk_put_global_string
(
ctx
,
"print"
);
duk_push_c_function
(
ctx
,
native_adder
,
DUK_VARARGS
);
duk_put_global_string
(
ctx
,
"adder"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment