fengari-lua / fengari

🌙 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser

Home Page:https://fengari.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Syntax error when attempting to use the `new` JS operator

Forcoy opened this issue · comments

local js = require("js")
local window = js.global

local function loadmod(path)
  local http = window.new window.XMLHttpRequest -- syntax error near 'http'
  http:open("GET",path,false)
  http:send(nil)
  print(http.responseText)
end

loadmod("http://127.0.0.1:5500/dumbtest.txt")

How do I avoid this? (moving window.new to be before the variable is set doesn't work either).

commented
local js = require "js"
local global = js.global
local function loadmod(url)
    local xhr = js.new(global.XMLHttpRequest)
    xhr.onload = function(e)
        if e.status >= 200 and e.status <= 299 then
            print(e.responseText)
            return true
        end
        print("error:" .. url)
    end
    xhr:open("GET", url, true)
    xhr:send()
end
loadmod("http://127.0.0.1:5500/dumbtest.txt")

Thank you!

xhr.onload = function(e)

Doesn't this have to be

xhr.onload = function(this, e)
or alternatively
function xhr:onload(e)