Skip to content

Commit e899a9f

Browse files
authored
Merge pull request #1 from d954mas/master
Add support for other platforms
2 parents 5e76b25 + a1858fe commit e899a9f

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

uuid4/src/uuid4.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,79 @@ void UUID4_PREFIX(seed)(uint64_t* state)
142142
*state = *state * 6364136223846793005u + (uintptr_t)UUID4_PREFIX(gen);
143143
}
144144

145+
#elif defined(__EMSCRIPTEN__)
146+
147+
#ifdef __cplusplus
148+
} // end of extern "C"
149+
#endif
150+
151+
#include <emscripten/emscripten.h>
152+
153+
#ifdef __cplusplus
154+
extern "C" {
155+
#endif
156+
157+
#include <ctime>
158+
#include <cstdlib>
159+
160+
UUID4_FUNCSPEC
161+
void UUID4_PREFIX(seed)(uint64_t* state)
162+
{
163+
static uint64_t state0 = 0;
164+
165+
//Returns the highest-precision representation of the current time that the browser provides.
166+
//This uses either Date.now or performance.now. The result is not an absolute time, and is only meaningful in comparison to other calls to this function.
167+
double now = emscripten_get_now();
168+
uint64_t time_component = (uint64_t)now;
169+
170+
// Initialize a random component based on current time
171+
srand((unsigned)time(NULL));
172+
uint64_t random_component = (uint64_t)rand();
173+
174+
// Use the address of a local volatile variable as a platform-specific value
175+
volatile int local_variable;
176+
uint64_t platform_value = (uint64_t)(&local_variable);
177+
178+
// Start combining the components to form the initial state
179+
*state = state0++ + time_component;
180+
181+
// Mix in the random and platform-specific components using hash and mix functions
182+
*state = *state * 6364136223846793005u + UUID4_PREFIX(mix)(UUID4_PREFIX(hash)((uint32_t)random_component), UUID4_PREFIX(hash)((uint32_t)platform_value));
183+
184+
// Further mix the state with the platform-specific value for additional uniqueness
185+
*state = *state * 6364136223846793005u + platform_value;
186+
}
187+
145188
#else
189+
#include <stdlib.h>
190+
#include <time.h>
191+
192+
UUID4_FUNCSPEC
193+
void UUID4_PREFIX(seed)(uint64_t* state)
194+
{
195+
static uint64_t state0 = 0;
196+
197+
// Get current time in seconds since the Unix epoch
198+
time_t now = time(NULL);
199+
uint64_t time_component = (uint64_t)now;
146200

147-
#error unsupported platform
201+
// Initialize a random component
202+
srand((unsigned)now); // Use the current time to seed the random number generator
203+
uint64_t random_component = (uint64_t)rand();
148204

205+
// Use the address of a local volatile variable as a platform-specific value
206+
volatile int local_variable;
207+
uint64_t platform_value = (uint64_t)(&local_variable);
208+
209+
// Start combining the components to form the initial state
210+
*state = state0++ + time_component;
211+
212+
// Mix in the random and platform-specific components
213+
*state = *state * 6364136223846793005u + UUID4_PREFIX(mix)(UUID4_PREFIX(hash)((uint32_t)random_component), UUID4_PREFIX(hash)((uint32_t)platform_value));
214+
215+
// Further mix the state with the platform-specific value for additional uniqueness
216+
*state = *state * 6364136223846793005u + platform_value;
217+
}
149218
#endif
150219

151220
#include <stdio.h>

0 commit comments

Comments
 (0)