Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't load Node-specific code when bundled via Webpack #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
UNRELEASED

- Fix Fengari loading Node-specific code when bundled for the web with Webpack
- Add os.setlocale that only understands the C locale
- Fix incorrect length for certain tables
- Remove luai_apicheck
Expand Down
7 changes: 7 additions & 0 deletions src/isnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

const isBrowser = ((typeof process === "undefined") || process.browser);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love a new source file for this. I think put it into defs.js?

const isNode = !isBrowser;

module.exports.isBrowser = isBrowser;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC don't use camelCase anywhere in fengari; please use snake_case for consistency.

module.exports.isNode = isNode;
7 changes: 5 additions & 2 deletions src/lauxlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const {
to_luastring,
to_uristring
} = require("./fengaricore.js");
const {
isBrowser
} = require("./isnode.js");

/* extra error code for 'luaL_loadfilex' */
const LUA_ERRFILE = LUA_ERRERR+1;
Expand Down Expand Up @@ -820,7 +823,7 @@ const skipcomment = function(lf) {

let luaL_loadfilex;

if (typeof process === "undefined") {
if (isBrowser) {
class LoadF {
constructor() {
this.n = NaN; /* number of pre-read characters */
Expand Down Expand Up @@ -989,7 +992,7 @@ const luaL_dofile = function(L, filename) {
const lua_writestringerror = function() {
for (let i=0; i<arguments.length; i++) {
let a = arguments[i];
if (typeof process === "undefined") {
if (isBrowser) {
/* split along new lines for separate console.error invocations */
do {
/* regexp uses [\d\D] to work around matching new lines
Expand Down
5 changes: 4 additions & 1 deletion src/lbaselib.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ const {
to_jsstring,
to_luastring
} = require("./fengaricore.js");
const {
isBrowser
} = require("./isnode.js");

let lua_writestring;
let lua_writeline;
if (typeof process === "undefined") {
if (isBrowser) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is actually checking for the process module (it uses process.stdout if available in the other branch)

if (typeof TextDecoder === "function") { /* Older browsers don't have TextDecoder */
let buff = "";
let decoder = new TextDecoder("utf-8");
Expand Down
5 changes: 4 additions & 1 deletion src/ldblib.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const {
luastring_indexOf,
to_luastring
} = require("./fengaricore.js");
const {
isNode
} = require("./isnode.js");

/*
** If L1 != L, L1 can be in any state, and therefore there are no
Expand Down Expand Up @@ -469,7 +472,7 @@ const dblib = {
};

let getinput;
if (typeof process !== "undefined") { // Only with Node
if (isNode) { // Only with Node
const readlineSync = require('readline-sync');
readlineSync.setDefaultOptions({
prompt: 'lua_debug> '
Expand Down
3 changes: 2 additions & 1 deletion src/linit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { lua_pop } = require('./lua.js');
const { luaL_requiref } = require('./lauxlib.js');
const { to_luastring } = require("./fengaricore.js");
const { isNode } = require("./isnode.js");

const loadedlibs = {};

Expand Down Expand Up @@ -36,7 +37,7 @@ loadedlibs[lualib.LUA_STRLIBNAME] = luaopen_string;
loadedlibs[lualib.LUA_MATHLIBNAME] = luaopen_math;
loadedlibs[lualib.LUA_UTF8LIBNAME] = luaopen_utf8;
loadedlibs[lualib.LUA_DBLIBNAME] = luaopen_debug;
if (typeof process !== "undefined")
if (isNode)
loadedlibs[lualib.LUA_IOLIBNAME] = require('./liolib.js').luaopen_io;

/* Extension: fengari library */
Expand Down
12 changes: 8 additions & 4 deletions src/loadlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ const {
to_uristring
} = require("./fengaricore.js");
const fengari = require('./fengari.js');
const {
isBrowser,
isNode
} = require("./isnode.js");

const global_env = (function() {
if (typeof process !== "undefined") {
if (isNode) {
/* node */
return global;
} else if (typeof window !== "undefined") {
Expand Down Expand Up @@ -120,7 +124,7 @@ const AUXMARK = to_luastring("\x01");
** error string in the stack.
*/
let lsys_load;
if (typeof process === "undefined") {
if (isBrowser) {
lsys_load = function(L, path, seeglb) {
path = to_uristring(path);
let xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -195,7 +199,7 @@ const noenv = function(L) {
};

let readable;
if (typeof process !== "undefined") { // Only with Node
if (isNode) { // Only with Node
const fs = require('fs');

readable = function(filename) {
Expand Down Expand Up @@ -270,7 +274,7 @@ const ll_loadlib = function(L) {
};

const env = (function() {
if (typeof process !== "undefined") {
if (isNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to leave this one as actually checking process?

/* node */
return process.env;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/loslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const {
to_jsstring,
to_luastring
} = require("./fengaricore.js");
const {
isBrowser
} = require("./isnode.js");

/* options for ANSI C 89 (only 1-char options) */
// const L_STRFTIMEC89 = to_luastring("aAbBcdHIjmMpSUwWxXyYZ%");
Expand Down Expand Up @@ -477,7 +480,7 @@ const syslib = {
"time": os_time
};

if (typeof process === "undefined") {
if (isBrowser) {
syslib.clock = function(L) {
lua_pushnumber(L, performance.now()/1000);
return 1;
Expand Down
5 changes: 4 additions & 1 deletion src/luaconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const {
LUA_VERSION_MINOR,
to_luastring
} = require('./defs.js');
const {
isBrowser
} = require("./isnode.js");

/*
** LUA_PATH_SEP is the character that separates templates in a path.
Expand Down Expand Up @@ -36,7 +39,7 @@ module.exports.LUA_EXEC_DIR = LUA_EXEC_DIR;
const LUA_VDIR = LUA_VERSION_MAJOR + "." + LUA_VERSION_MINOR;
module.exports.LUA_VDIR = LUA_VDIR;

if (typeof process === "undefined") {
if (isBrowser) {
const LUA_DIRSEP = "/";
module.exports.LUA_DIRSEP = LUA_DIRSEP;

Expand Down
5 changes: 4 additions & 1 deletion src/lualib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
LUA_VERSION_MAJOR,
LUA_VERSION_MINOR
} = require("./lua.js");
const {
isNode
} = require("./isnode.js");

const LUA_VERSUFFIX = "_" + LUA_VERSION_MAJOR + "_" + LUA_VERSION_MINOR;
module.exports.LUA_VERSUFFIX = LUA_VERSUFFIX;
Expand All @@ -20,7 +23,7 @@ const LUA_TABLIBNAME = "table";
module.exports.LUA_TABLIBNAME = LUA_TABLIBNAME;
module.exports.luaopen_table = require("./ltablib.js").luaopen_table;

if (typeof process !== "undefined") {
if (isNode) {
const LUA_IOLIBNAME = "io";
module.exports.LUA_IOLIBNAME = LUA_IOLIBNAME;
module.exports.luaopen_io = require("./liolib.js").luaopen_io;
Expand Down