From d8c3bb9264865a927c346d27ceff72e360fd117c Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 14 Sep 2018 13:45:27 -0700 Subject: [PATCH] Convert Promise.Status to be a userdata with __tostring --- lib/init.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 7cedb40..185d13c 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -62,14 +62,26 @@ local function isEmpty(t) return next(t) == nil end +local function createSymbol(name) + assert(type(name) == "string", "createSymbol requires `name` to be a string.") + + local symbol = newproxy(true) + + getmetatable(symbol).__tostring = function() + return ("Symbol(%s)"):format(name) + end + + return symbol +end + local Promise = {} Promise.prototype = {} Promise.__index = Promise.prototype Promise.Status = { - Started = "Started", - Resolved = "Resolved", - Rejected = "Rejected", + Started = createSymbol("Started"), + Resolved = createSymbol("Resolved"), + Rejected = createSymbol("Rejected"), } --[[